Search code examples
doctrine-ormdoctrinedql

Doctrine Query, association ManyToOne


I have 2 entities:

Class A {

    /**
     * @ORM\ManyToOne(...)
     */
    private $b;

    /**
     * @ORM\Column(name="date", type="date")
     */
    private $date;
}

Class B {

    /**
     * @ORM\OneToMany(...)
     */
    private $a;

}

Need a Query for get all entities "B" that yours "A" have all dates "date" below to date that I set as parameter.

Now have these Query, but is no result:

"SELECT b FROM Entity:B b INNER JOIN b.a a WHERE a.date > :date"


Solution

  • You could do something like the following :

    public function myfunction($date){
        $dql = "SELECT b FROM Entity:B as b WHERE b.a IN (".
                   "SELECT a.id FROM Entity:A as a WHERE a.date > :date".
               ")";
        $query = $this->getEntityManager()
            ->createQuery($dql)
            ->setParameter('date', $date)
        ;
    
        return $query->getResult();
    }
    

    I tested on some of my entities that have similar structure and it works