简体   繁体   English

DOCTRINE2 oneToMany与条件的关系-无结果

[英]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 例如:我有实体Post ... Post具有实体Comment的集合-关系是oneToMany注释可以通过参数deleteAt删除,默认为NULL Comment具有另一个实体B的集合-关系是oneToMany

I made optimalization for querybuilder: 我对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');
  • this SQL works, if all comments are not deleted 如果未删除所有注释,则此SQL有效
  • when all comments are deleted, then I have no result 当所有评论都删除时,我没有结果

how to solve it ? 怎么解决呢?

Move the deletedAt check to the join: deletedAt检查移至deletedAt

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM