我应该使用哪种最佳做法?

时间:2016-01-27 19:09:36

标签: mysql symfony doctrine-orm

我是Symfony2的新手。 我的表中有5列:answer_a,answer_b,answer_c,answer_d和correct_answer。

答案a-d在ABCD测验中包含文本答案。在correct_answer中,我有" a"," b"," c"或" d"串。我怎样才能以最好的方式向用户显示正确的文字答案?我应该添加实体new" text_correctanswer"领域?或者我应该在控制器中创建具有正确文本答案的新数组?

1 个答案:

答案 0 :(得分:0)

您需要oneToMany关系

  

Documentation with examples

use Doctrine\Common\Collections\ArrayCollection;

/** @Entity */
class Quiz {
    protected $id;

    /**
     * @OneToMany(targetEntity="Answer", mappedBy="quiz")
     */
    protected $answers;

    public function __construct()
    {
        $this->answers = new ArrayCollection;
    }
}

/** @Entity */
class Answer {
    protected $id;
    protected $isCorrect;

    /**
     * @ManyToOne(targetEntity="Quiz", inversedBy="answers")
     * @JoinColumn(name="quiz_id", referencedColumnName="id")
     */
    protected $quiz;
}