Doctrine2 - get group which hasn't hidden person

81 Views Asked by At

The first entity called BUNDLE

/**
 * @var \Doctrine\Common\Collections\Collection of \Akademie\Course\Course
 * @ManyToMany(targetEntity="Akademie\Course\Course", mappedBy="bundles")
 */
private $courses;

The second entity is called COURSE

/**
 * @var \Doctrine\Common\Collections\Collection of \Akademie\Bundle\Bundle
 * @ManyToMany(targetEntity="Akademie\Bundle\Bundle", inversedBy="courses")
 */
    private $bundles;

/**
 * @var \Doctrine\Common\Collections\Collection of \Akademie\Course\CourseDate
 * @OneToMany(targetEntity="Akademie\Course\CourseDate", mappedBy="course")
 */
    private $courseDates;

/**
 * @var int
 * @Column(type="boolean")
 */
    private $hidden;

and the third is called COURSEDATE

/**
 * @var \Akademie\Course\Course
 * @ManyToOne(targetEntity="Akademie\Course\Course", inversedBy="courseDates")
 * @JoinColumn(nullable=false)
 */
    private $course;

/**
 * @var \DateTime
 * @Column(type="datetimetz", nullable=true)
 */
    private $beginDate;

I have parameter course and I need to get all bundles, which contains this course. What's more, all other courses in that bundle has to have courseDate newer than current date and can't be hidden. Otherwise I don't want to get this bundle. I hope it is clear now...

1

There are 1 best solutions below

5
On

I am not familiar with MEMBER OF but i think you are better of with an INNER JOIN.

DQL:

SELECT group
FROM path\to\entity\Group group
INNER JOIN group.persons person
INNER JOIN person.city city
WHERE person.hidden = FALSE AND city.name = :name

QueryBuilder:

$em->createQueryBuilder()
    ->select('group')
    ->from('path\to\entity\Group', 'group')
    ->innerJoin('group.persons', 'person')
    ->innerJoin('person.city', 'city')
    ->where('person.hidden = FALSE')
    ->andWhere('city.name = :name')
    ->setParameter('name', $yourName)
    ->getQuery()->getResult();

I assumed that your person entity has an attribute called hidden and that your person-city relationship is bidirectional.