小编Bel*_*sus的帖子

从Doctrine2和Symfony2获取与多向对话的单向实体

我目前正在开展语言评估项目,使您能够以您想要的语言参加考试并评估您的水平.我使用Symfony2框架并与Doctrine2一起使用.我的问题是以下问题:

我有两个实体考试和问题由多对多关系链接(考试是所有者).每个考试都可以与几个问题相关,每个问题都可以与几个考试相关.

这是我的代码:

考试实体

/**
 * Exam
 *
 * @ORM\Table(name="cids_exam")
 * @ORM\Entity(repositoryClass="LA\AdminBundle\Entity\ExamRepository")
 */
class Exam
{
    ...

    /**
    * @ORM\ManyToMany(targetEntity="LA\AdminBundle\Entity\Question", cascade={"persist"})
    * @ORM\JoinTable(name="cids_exam_question")
    */
    private $questions;

    ...


    /**
     * Constructor
     */
    public function __construct()
    {
        $this->questions = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Add questions
     *
     * @param \LA\AdminBundle\Entity\Question $questions
     * @return Exam
     */
    public function addQuestion(\LA\AdminBundle\Entity\Question $questions)
    {
        $this->questions[] = $questions;

        return $this;
    }

    /**
     * Remove questions
     *
     * @param \LA\AdminBundle\Entity\Question $questions
     */
    public function removeQuestion(\LA\AdminBundle\Entity\Question $questions)
    {
        $this->questions->removeElement($questions); …
Run Code Online (Sandbox Code Playgroud)

many-to-many symfony doctrine-orm

6
推荐指数
1
解决办法
9935
查看次数

标签 统计

doctrine-orm ×1

many-to-many ×1

symfony ×1