pooler
pooler

Reputation: 337

DOCTRINE2 oneToMany relationship with condition - no result

for example: I have entity Post ... Post have collection of entity Comment - relationship is oneToMany Comments can be deleted by parameter deletedAt, which is default NULL Comment have another collection of entity B - relationship is oneToMany

I made optimalization for querybuilder:

$qb = $this->createQueryBuilder('post');
$qb->select('post, comments, objectsOfB')
    ->andWhere('post.id = :id')->setParameter('id', $postId)
    ->leftJoin('post.comments', 'comments')
    ->andWhere('comments.deletedAt is NULL')
    ->leftJoin('comments.objectsOfB', 'objectsOfB');

how to solve it ?

Upvotes: 0

Views: 108

Answers (1)

Amir Rahimi Farahani
Amir Rahimi Farahani

Reputation: 1590

Move the deletedAt check to the join:

$qb = $this->createQueryBuilder('post');
$qb->select('post, comments, objectsOfB')
    ->andWhere('post.id = :id')->setParameter('id', $postId)
    ->leftJoin('post.comments', 'comments', 'WITH', 'comments.deletedAt is NULL')
    ->leftJoin('comments.objectsOfB', 'objectsOfB');

Upvotes: 1

Related Questions