例如:我有实体帖子...帖子收集实体评论 - 关系是OneTomany可以通过参数deletedat删除注释,默认为null评论还有另一个实体b-关系是Onetomany
我为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');
- 此SQL有效,如果所有注释均未删除
- 当所有注释被删除时,我就没有结果
如何解决它?
将deletedAt
移动到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');