我有一个包含3个实体的测验结构:
代码如下:
测验实体
class Quiz
{
/**
* @ORM\OneToMany(targetEntity="Question", mappedBy="quiz", cascade={"persist", "remove"})
*/
private $questions;
public function __construct() {
$this->questions = new ArrayCollection();
}
public function addQuestion(\Cariboo\QuizBundle\Entity\Question $questions)
{
$questions->setQuiz( $this );
// die(); Not dying here...
$this->questions[] = $questions;
return $this;
}
public function removeQuestion(\Cariboo\QuizBundle\Entity\Question $questions)
{
$this->questions->removeElement($questions);
}
public function getQuestions()
{
return $this->questions;
}
}
问题实体
class Question
{
/**
* @ORM\OneToMany(targetEntity="Answer", mappedBy="question", cascade={"persist", "remove"})
*/
private $answers;
/**
* @ORM\ManyToOne(targetEntity="Cariboo\QuizBundle\Entity\Quiz", cascade={"persist"})
*/
private $quiz;
public function addAnswer(\Cariboo\QuizBundle\Entity\Answer $answers)
{
$answers->setQuestion( $this );
$this->answers[] = $answers;
return $this;
}
public function removeAnswer(\Cariboo\QuizBundle\Entity\Answer $answers)
{
$this->answers->removeElement($answers);
}
public function getAnswers()
{
return $this->answers;
}
public function setQuiz(\Cariboo\QuizBundle\Entity\Quiz $quiz = null)
{
$this->quiz = $quiz;
return $this;
}
public function getQuiz()
{
return $this->quiz;
}
}
回答实体
class Answer
{
/**
* @ORM\ManyToOne(targetEntity="Cariboo\QuizBundle\Entity\Question")
*/
private $question;
public function setQuestion(\Cariboo\QuizBundle\Entity\Question $question = null)
{
$this->question = $question;
return $this;
}
public function getQuestion()
{
return $this->question;
}
}
我级联坚持他们。
我按照Cascaded persist not working (Doctrine ORM + Symfony 2)
中的说明配置了我的二传手执行addAnswer并获得答案,问题设置正确。但是,addQuestion没有设置测验。
image of what gets dumped upon sending the form
我可以在我的控制器中添加foreach
,但我对此并不满意,因为我觉得我没有利用symfony的力量。 (没有Entity doesn't associate correctly的重复)
if ( $form->isValid() ) {
foreach ( $quiz->getQuestions() as $question ) {
$question->setQuiz( $quiz );
}
$em = $this->getDoctrine()->getManager();
$em->persist( $quiz );
$em->flush();
return $this->redirect( $this->generateUrl( 'quiz_show', array(
'slug' => $quiz->getSlug()
) ) );
}
我无法弄清楚为什么addQuestion
没有被执行。
我的Quiz
表格
$builder
->add( 'questions', 'collection', array(
'type' => new QuestionType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'prototype_name' => '__qst_prot__'
) )
;
Question
$builder
->add( 'question', 'text' )
->add( 'answers', 'collection', array(
'type' => new AnswerType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'prototype_name' => '__asw_prot__'
) );
我的Answer form
$builder
->add( 'answer', 'text' )
->add( 'correct' )
;
答案 0 :(得分:2)
问题出在collection
表单类型中。如果要调用父实体设置器,则需要将by_reference
设置为false
。
Symfony docs:
Similarly, if you're using the CollectionType field where your underlying collection data is an object (like with Doctrine's ArrayCollection), then by_reference must be set to false if you need the adder and remover (e.g. addAuthor() and removeAuthor()) to be called.
在此处阅读更多内容:http://symfony.com/doc/current/reference/forms/types/collection.html#by-reference