Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine DQL: how to get expression to inversed side

I wish to fetch an entity where the inversed association does not exist (on a 1:1 association)

I get the error:

A single-valued association path expression to an inverse side is not supported in DQL queries. Use an explicit join instead.

Query:

$query = $this->getEntityManager()->createQuery("
                SELECT DISTINCT(p.date)
                FROM MainBundle:Price p
                WHERE p.emaPrice IS NULL
                ORDER BY p.date ASC
            ")
            ->setMaxResults(1);
        $date = $query->getOneOrNullResult();

I understand the error, but I'm really stuck on fixing it. I've read I should add an explicit JOIN, but I've added it and still no luck.

EDIT query with join:

$query = $this->getEntityManager()->createQuery("
        SELECT DISTINCT(p.date)
        FROM MainBundle:Price p
        JOIN MomentumBundle:EmaPrice ep
        WITH ep.id = p.emaPrice
        WHERE p.emaPrice IS NULL
        ORDER BY p.date ASC
    ")
    ->setMaxResults(1);
$date = $query->getOneOrNullResult();
like image 989
Tjorriemorrie Avatar asked Aug 05 '13 12:08

Tjorriemorrie


1 Answers

I would like to thank @Flip and @lifo for their great support in helping me resolve this:

$query = $this->getEntityManager()->createQuery("
        SELECT DISTINCT(p.date)
        FROM MainBundle:Price p
        LEFT JOIN MomentumBundle:EmaPrice ep
        WITH ep.price = p.id
        WHERE ep IS NULL
        ORDER BY p.date ASC
    ")
    ->setMaxResults(1);
$date = $query->getOneOrNullResult();
like image 162
Tjorriemorrie Avatar answered Sep 28 '22 22:09

Tjorriemorrie