Rey*_*apt 5 php symfony doctrine-orm
我有一个包含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;
}
}
Run Code Online (Sandbox Code Playgroud)
问题实体
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;
}
}
Run Code Online (Sandbox Code Playgroud)
回答实体
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;
}
}
Run Code Online (Sandbox Code Playgroud)
我级联坚持他们.
我按照Cascaded persist not working(Doctrine ORM + Symfony 2)中的说明配置了我的setter
执行addAnswer并获得答案,问题设置正确.但是addQuestion没有设置测验.
我可以foreach在我的控制器中添加一个,但我对此不满意,因为我觉得我没有利用symfony的力量.(没有重复的实体没有正确关联)
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()
) ) );
}
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚为什么addQuestion不执行.
我的Quiz表格
$builder
->add( 'questions', 'collection', array(
'type' => new QuestionType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'prototype_name' => '__qst_prot__'
) )
;
Run Code Online (Sandbox Code Playgroud)
部分建设者 Question
$builder
->add( 'question', 'text' )
->add( 'answers', 'collection', array(
'type' => new AnswerType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'prototype_name' => '__asw_prot__'
) );
Run Code Online (Sandbox Code Playgroud)
我的 Answer form
$builder
->add( 'answer', 'text' )
->add( 'correct' )
;
Run Code Online (Sandbox Code Playgroud)
问题出在collection表单类型上。如果要调用父实体setter,则需要设置by_reference为。false
Symfony 文档:
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
| 归档时间: |
|
| 查看次数: |
148 次 |
| 最近记录: |