D
D
Dmitry Marinov2015-05-10 18:12:52
Doctrine ORM
Dmitry Marinov, 2015-05-10 18:12:52

Zend Framework 2 how to implement polls module?

I am doing a project on Zend Fremework 2 + Doctrine ORM. There is a task to write the designer of polls. A survey can have an unlimited number of questions. The answer to a question can have one of 5 types: Text, Textarea, Radio, MultiCheckbox ,Select. Multiple choice options can be unlimited. I sort of solved the problem, but with a crutch. I'm sure there is a native solution based on collections, but I can't figure it out.
So, now the structure is as follows (The names of the models are fictitious to make it easier to understand):

  • Model Interview (id, name of the survey)
  • AnswerType (Types of answers)
  • InterviewQuestion(Questions linked to Interview and AnswerType)
  • InterviewQuestionAnswer (Answer options linked to InterviewQuestion)
  • InterviewUserAnswer (User responses linked to InterviewQuestion, InterviewQuestionAnswe, Userr)

Fieldsets are generated when the response form is generated. Example:
if (!empty($this->questions)) {
    foreach ($this->questions as $question) {
        switch ($question->getQuestionType()->getTypeName()) {
            case 'Text':
                $this->add(array(
                    'name'       => $question->getId(),
                    'type'       => $question->getQuestionType()->getTypeName(),
                    'options'    => array(
                        'label' => $question->getQuestionText(),
                    ),
                    'attributes' => array(
                        'class' => 'form-control'
                    ),
                ));
                break;

            case 'MultiCheckbox':
                $this->add(array(
                    'name'       => $question->getId(),
                    'type'       => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
                    'options'    => array(
                        'label'          => $question->getQuestionText(),
                        'object_manager' => $objectManager,
                        'target_class'   => 'Category\Entity\RegAppAnswers',
                        'property'       => 'answer',
                        'is_method'      => true,
                        'find_method'    => array(
                            'name'   => 'findBy',
                            'params' => array(
                                'criteria' => array('question' => $question->getId()),
                            ),
                        ),
                    ),
                    'attributes' => array(
                        'class' => 'checkbox-inline'
                    ),
                ));

                break;
        }
    }
}

Everything is going well until the shape is maintained. When saving, validation successfully passes, but I insert records into the database through foreach.
Maybe someone had a similar problem, or is there a better algorithm? I did not find a ready-made module.
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question