Get entities from a unidirectional many to many relation with Doctrine2 and Symfony2

十年热恋 提交于 2019-12-05 06:08:04

To have a findByExam() method in your QuestionRepository do the following:

 public function findByExam($exam)
 {
    $q = $this->createQueryBuilder('q')
        ->where('q.exam = :exam')
        ->setParameter('exam', $exam)
        ->getQuery();

    return $q->getResult();
 }

You could also create a bi-directional relationship not uni-directional !

Each exam can be related to several questions, and each question can be related to several exams.

Create a bi-directional relationship by adding this to your Question entity:

use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use Vendor\YourExamBundle\Entity\ExamInterface;

class Question 
{
    protected $exams;

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

    public function getExams()
    {
       return $this->exams;
    }

    public function addExam(ExamInterface $exam)
    {
        if !($this->exams->contains($exam)) {
            $this->exams->add($exam);
        }
        return $this;
    }

    public function setExams(Collection $exams)
    {
        $this->exams = $exams;

        return $this;
    }

    // ...

Afterwards you can use...

$question->getExams()

... in your controller.

To automatically join your related entities doctrine's fetch option can be used with:

  • LAZY ( loads the relations when accessed )
  • EAGER ( auto-joins the relations )
  • EXTRA_LAZY ( manual fetching )

example:

/**
 * @ManyToMany(targetEntity="Question",inversedBy="exams", cascade={"all"}, fetch="EAGER")
 */

Though eager loading has a downside in terms of performance it might be an option for you.

Doctrine Fetch with EAGER

Whenever you query for an entity that has persistent associations and these associations are mapped as EAGER, they will automatically be loaded together with the entity being queried and is thus immediately available to your application.

Read more about it in the Doctrine Documentation.

Another option you should check when working with relations is the cascade option.

See the Doctrine - Working with Associations chapter of the documentation.

Tip: You should create interfaces for exams and questions and use them instead of the original entity in your set and add methods to allow easier extending.

Bi-Directional Relations using Doctrine2 ORM with association table exam_questions exam_id question_id

<?php

class Exams

 ....OTHER PROPERTIES...

 /**
  * Owning Side
  *
  * @ManyToMany(targetEntity="Questions", inversedBy="exams")
  * @JoinTable(name="exam_questions",
  *      joinColumns={@JoinColumn(name="exam_id", referencedColumnName="id")},
  *      inverseJoinColumns={@JoinColumn(name="question_id", referencedColumnName="id")}
  *      )
  */
 private $questions;

 ..OTHER CODES..

 }


 class Questions{


 ..OTHER CODES..

 /**
  * Inverse Side
  *
  * @ManyToMany(targetEntity="Exams", mappedBy="questions")
  */
 private $exams;

 ..OTHER CODES..

 }

http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/annotations-reference.html#annref-manytomany

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!