A
A
ak_wi2019-08-24 21:15:36
symfony
ak_wi, 2019-08-24 21:15:36

Symfony: How to create a poll form with ChoiceType?

There are three classes:
1. Poll. Contains an ArrayCollection with questions
2. Question. Contains an ArrayCollection with
3 answers. Answer. Contains only answer text The
question is how to make a form for displaying a survey with questions and answer options so that each question is ChoiceType | EntityType | form with checkmarks, where the options were the answers?
And what is the best way to store results data? At the moment, a head-on solution is used, when using a separate entity, the user id, id-of the selected answer options and the time of the survey are saved.

class Quiz
{
 /**
     * @ORM\OneToMany(targetEntity="App\Entity\Polls\Question", mappedBy="quiz", cascade={"persist"})
     */
    private $questions;
}

class Question
{

/**
     * @ORM\ManyToOne(targetEntity="App\Entity\Polls\Quiz", inversedBy="questions", cascade={"persist"})
     */
    private $quiz;

 /**
     * @ORM\OneToMany(targetEntity="App\Entity\Polls\Answer", mappedBy="question", cascade={"persist"})
     */
    private $answers;
}

class Answer
{

 /**
     * @ORM\Column(type="text")
     */
    private $text;

/**
     * @ORM\ManyToOne(targetEntity="App\Entity\Polls\Question", inversedBy="answers", cascade={"persist"})
     */
    private $question;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
postgresdev, 2019-09-14
@postgresdev

Use Collection Type:
https://symfony.com/doc/current/reference/forms/ty...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question