在Symfony中,我有两个实体:问题和答案。它们具有一对多的关系,其中一个问题可以有很多答案。使用thees实体我正在尝试构建一个带有文本字段的表单,用户可以在其中编辑来自实体的数据。阅读Symfony食谱文档我明白我需要使用一组表单来显示问题的多个答案。根据我的理解,问题表单将生成问题数据,这也将嵌入具有所有答案的答案表单。此时我遇到了这个错误:
无法加载类型" QuizBundle \ Entity \ Answer
有人可以帮我解决这个问题并用问题和答案构建表格吗?感谢
这是我的答案实体:
/**
* 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
*
* @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;
}
}
问题表格:
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';
}
}
答案表格:
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';
}
}
控制器代码:
public function adminEditRecordAction(){
尝试从DB和答案中获取问题
$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()));
}
答案 0 :(得分:0)
您的表单类中有错误:
HTML 5
正如Symfony's docs所说:
这是此集合中每个项目的字段类型(,例如TextType,ChoiceType等)。 (...)
您提供了实体的类名($builder->add('answers', CollectionType::class, array('entry_type' => Answer::class));
),而它应该是某个Answer::class
的类名(例如FormType
)。