我有两个关系为OneToMany
的实体。
实体问题:
/**
* @ORM\OneToMany(targetEntity="Quiz\CoreBundle\Entity\Answer", mappedBy="question", cascade={"persist"})
*/
private $answers;
实体回答:
/**
* @ORM\ManyToOne(targetEntity="Quiz\CoreBundle\Entity\Question", inversedBy="answers")
*/
private $question;
我试着坚持下去:
$em = $this->getDoctrine()->getManager();
$question = new Question();
$answer = new Answer();
$answer2 = new Answer();
$answer->setAnswerText('Roterdam');
$answer2->setAnswerText('Amsterdam')
->SetCorrect(true);
$question->setQuestionText('What\'s the capital of Netherlands? ');
$question->addAnswer($answer);
$question->addAnswer($answer2);
$em->persist($question);
$em->flush();
当我运行此代码时,除了答案表中的外键之外,db中的所有内容都会更新,question_id
为空。
知道我做错了什么吗?
答案 0 :(得分:1)
这必须是五大最受欢迎的Doctrine 2问题之一。但是我懒得找一个链接到。
问问自己答案如何知道它属于哪个问题?对象级别的链接在哪里?
class Question
{
function addAnswer($answer)
{
$this->answers[] = $answer;
$answer->setQuestion($this);
}
}