2016-06-21 4 views
0

zum Beispiel: Ich habe Entität Beitrag ... Beitrag Sammlung von Entität Kommentar haben - Beziehung ist OneToMany Kommentare können durch Parameter deletedAt gelöscht werden, die standardmäßig NULL Kommentar eine weitere Sammlung haben, ist von Einheit B - ist BeziehungDoctrine2 OneToMany Beziehung mit Zustand - kein Ergebnis

I Optimalisierung für Querybuilder gemacht OneToMany:

$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'); 
  • diese SQL funktioniert, wenn alle Kommentare nicht gelöscht werden
  • wenn alle Kommentare gelöscht werden, dann habe ich kein Ergebnis

wie es zu lösen?

Antwort

1

Bewegen Sie den deletedAt Scheck an den beitreten:

$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');