具有关系的持久化对象,数据库不更新

Persisting object with relationship, database not updating

我有 2 个实体具有关系 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();

当我 运行 此代码在数据库中更新除了答案 table 中的外键之外的所有内容时,question_id 为空。

知道我做错了什么吗?

这一定是 Doctrine 2 最受欢迎的五个问题之一。但是我懒得去查一个到 link 到.

问问自己答案是如何知道它属于哪个问题的?对象级别的 link 在哪里?

class Question
{
    function addAnswer($answer)
    {
        $this->answers[] = $answer;
        $answer->setQuestion($this);
    }
}