简体   繁体   English

Symfony表单集合

[英]Symfony form collection

In Symfony I have two entities: Question and Answer. 在Symfony中,我有两个实体:问题和答案。 They have a relation of one to many, where one question can have many answers. 它们具有一对多的关系,其中一个问题可以有很多答案。 Using thees entities I am trying to build a form with text fields where the user can edit data from thees entities. 使用thees实体我正在尝试构建一个带有文本字段的表单,用户可以在其中编辑来自实体的数据。 Reading the Symfony cookbook documentation I understand that I need to use a collection of forms to display the multiple answers for a question. 阅读Symfony食谱文档我明白我需要使用一组表单来显示问题的多个答案。 For what I understand the question form will generate the question data, and this will also embed the Answer form that will have all the answers. 根据我的理解,问题表单将生成问题数据,这也将嵌入具有所有答案的答案表单。 At this point I am stuck with this error: 此时我遇到了这个错误:

Could not load type "QuizBundle\\Entity\\Answer 无法加载类型“QuizBundle \\ Entity \\ Answer

Can someone help me solve this and build the form with the question and the answers? 有人可以帮我解决这个问题并用问题和答案构建表格吗? Thanks 谢谢

Here is my Answer Entity: 这是我的答案实体:

/**
 * Answer
 *
 * @ORM\Table(name="answer")
 * @ORM\Entity(repositoryClass="QuizBundle\Repository\AnswerRepository")
 */
class Answer
{
    /**
     * @ORM\ManyToOne(targetEntity="Question", inversedBy="answers")
     * @ORM\JoinColumn(name="question_id", referencedColumnName="id", onDelete="CASCADE")
     */
    private $question;

    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="answer", type="string", length=255)
     */
    private $answer;

    /**
     * @var bool
     *
     * @ORM\Column(name="isCorrect", type="boolean")
     */
    private $isCorrect;

    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set answer
     *
     * @param string $answer
     *
     * @return Answer
     */
    public function setAnswer($answer)
    {
        $this->answer = $answer;

        return $this;
    }

    /**
     * Get answer
     *
     * @return string
     */
    public function getAnswer()
    {
        return $this->answer;
    }

    /**
     * Set isCorrect
     *
     * @param boolean $isCorrect
     *
     * @return Answer
     */
    public function setIsCorrect($isCorrect)
    {
        $this->isCorrect = $isCorrect;

        return $this;
    }

    /**
     * Get isCorrect
     *
     * @return bool
     */
    public function getIsCorrect()
    {
        return $this->isCorrect;
    }

    /**
     * Set question
     *
     * @param \QuizBundle\Entity\Question $question
     *
     * @return Answer
     */
    public function setQuestion(\QuizBundle\Entity\Question $question = null)
    {
        $this->question = $question;
        return $this;
    }
    /**
     * Get question
     * @return \QuizBundle\Entity\Question
     */
    public function getQuestion()
    {
        return $this->question;
    }
}

Question Entity: 问题实体:

/**
 * Question
 *
 * @ORM\Table(name="question")
 * @ORM\Entity(repositoryClass="QuizBundle\Repository\QuestionRepository")
 */
class Question
{
    /**
     * @ORM\OneToMany(targetEntity="Answer", mappedBy="question", cascade={"remove"}, orphanRemoval=true)
     */
    private $answers;
    public function __construct()
    {
        $this->answers = new ArrayCollection();
    }

    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="image", type="string", length=255)
     */
    private $image;

    /**
     * @var string
     *
     * @ORM\Column(name="question", type="string", length=255)
     */
    private $question;

    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set image
     *
     * @param string $image
     *
     * @return Question
     */
    public function setImage($image)
    {
        $this->image = $image;

        return $this;
    }

    /**
     * Get image
     *
     * @return string
     */
    public function getImage()
    {
        return $this->image;
    }

    /**
     * Set question
     *
     * @param string $question
     *
     * @return Question
     */
    public function setQuestion($question)
    {
        $this->question = $question;

        return $this;
    }

    /**
     * Get question
     *
     * @return string
     */
    public function getQuestion()
    {
        return $this->question;
    }

    /**
     * Add answer
     *
     * @param \QuizBundle\Entity\Answer $answer
     *
     * @return Question
     */
    public function addAnswer(\QuizBundle\Entity\Answer $answer)
    {
        $this->answers[] = $answer;
        return $this;
    }

    /**
     * Remove answer
     *
     * @param \QuizBundle\Entity\Answer $answer
     */
    public function removeAnswer(\QuizBundle\Entity\Answer $answer)
    {
        $this->answers->removeElement($answer);
    }
    /**
     * Get answers
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getAnswers()
    {
        return $this->answers;
    }
}

Question Form: 问题表格:

class QuestionFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('image');
        $builder->add('question');
        $builder->add('answers', CollectionType::class, array('entry_type' => Answer::class));
    }
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array('data_class' => 'QuizBundle\Entity\Question'));
    }
    public function getName()
    {
        return 'quiz_bundle_question_form_type';
    }
}

Answer Form: 答案表格:

class AnswerFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('answer');

    }
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array('data_class' => 'QuizBundle\Entity\Answer'));
    }
    public function getName()
    {
        return 'quiz_bundle_answer_form_type';
    }
}

Controller Code: 控制器代码:

public function adminEditRecordAction(){

trying to get a question from DB and the answers 试图从数据库和答案中得到一个问题

        $question = $this->getDoctrine()->getRepository('QuizBundle:Question')->findOneByIdJoinedToCategory(1);

        $question = new Question();

        $answer = new Answer();
        $answer->setAnswer('answerrr1');
        $answer->setIsCorrect(false);
        $question->getAnswers()->add($answer);
        $form = $this->createForm(QuestionFormType::class, $question);
        return $this->render('QuizBundle:Default:editRecord.html.twig', array('form' => $form->createView()));
    }

You have an error in your form class here: 您的表单类中有错误:

$builder->add('answers', CollectionType::class, array('entry_type' => Answer::class));

As Symfony's docs say: 正如Symfony的文档所说:

This is the field type for each item in this collection ( eg TextType, ChoiceType, etc ). 这是此集合中每个项目的字段类型例如TextType,ChoiceType等 )。 (...) (......)

You've provided entity's classname ( Answer::class ) while it should be some FormType 's classname (eg AnswerType:::class ). 你提供了实体的类名( Answer::class ),而它应该是一些FormType的类名(例如AnswerType:::class )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM