doujia2463
doujia2463
2013-07-02 14:49

使用QueryBuilder将NOT IN与两个实体匹配

I have two entities ServeApp and App. They both have a relation to AppPage. I'm trying to get ServeApps that have AppPages which App does not have.

So ServeApps.appPages should not contain the same AppPages that Apps.appPages contains.

I'm trying to write a DQL query that would be something similar to

SELECT ServeApps WHERE ServeApps.appPages NOT IN Apps.appPages

but I'm at a loss how to go about it. I tried $queryBuilder->whereNotIn() but I guess that method doesn't exists?

Any help would be appreciated :)

EDIT:

I ended up changing my approach. Since the AppPages had a many to many relationship to both ServeApps and Apps, I did the following:

SELECT sa FROM ServeApps sa WHERE sa.id NOT IN (
  SELECT ssa.id FROM AppPage ap JOIN ap.serveApps ssa WHERE ap.id = :apppage
)

I changed my logic to take out the Apps from the equation and then I checked it differently. I also had a version where I resulted to raw SQL for counting the ServeApps.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dongyi1939 dongyi1939 8年前

    Your where-not-in statement should look like this

    $queryBuilder->where($queryBuilder->expr()->notIn('alias.field', $arrayOfValues));
    

    You can find a reference of all expressions in Doctrine's documentation.

    As a side note, a where-in statement should look like this

    $queryBuilder->where($queryBuilder->expr()->in('alias.field', $arrayOfValues));
    
    点赞 评论 复制链接分享

相关推荐