学说选择innerJoined实体或没有关联的实体
Posted
技术标签:
【中文标题】学说选择innerJoined实体或没有关联的实体【英文标题】:Doctrine select either innerJoined entities OR entities having no association 【发布时间】:2018-07-19 10:14:10 【问题描述】:我有以下与用户和地址的关联模型:
User
- id
- email
- password
Address
- id
- street
- zip_code
- city
- user_id
One user has [0:n] Address :
users (id, email, password)
======================================
| 1 | "someone1@example.org" | "..." |
| 2 | "someone2@example.org" | "..." |
| 3 | "someone3@example.org" | "..." |
| 4 | "someone4@example.org" | "..." |
| 5 | "someone5@example.org" | "..." |
======================================
addresses (id, street, zip_code, city, user_id)
===================================================
| 1 | "Somewhere" | "00001" | "City 1" | 1 |
| 2 | "Somewhere else" | "00002" | "City 2" | 1 |
| 3 | "Somewhere" | "00003" | "City 3" | 1 |
| 4 | "Somewhere else" | "00001" | "City 1" | 2 |
| 5 | "Somewhere" | "00002" | "City 2" | 2 |
| 6 | "Somewhere else" | "00003" | "City 3" | 2 |
| 7 | "Somewhere" | "00001" | "City 1" | 3 |
| 8 | "Somewhere else" | "00003" | "City 3" | 3 |
| 9 | "Somewhere" | "00002" | "City 2" | 4 |
| 10 | "Somewhere else" | "00003" | "City 3" | 4 |
===================================================
我想选择地址在“City 1”的用户:addresses.id IN (1, 4, 7),出于某种原因,我还需要包括地址正好为 0 的用户。
==> [1(地址#1)、2(地址#4)、3(地址#7)和5(无地址)],但不是用户4(有地址但不匹配)。
这是我尝试过的几个查询...
-
内连接
==> [1(地址#1),2(地址#4),3(地址#7)](但不是用户5)
$queryBuilder = $this->em->createQueryBuilder('u');
$queryBuilder
->innerJoin('u.addresses', 'a', \Doctrine\ORM\Query\Expr\Join::WITH, $queryBuilder->expr()->in('a.id', ':ids'))
->setParameter('ids', [1, 4, 7])
;
-
左连接
==> [ 1(地址#1,#2,#3),2(地址#4,#5,#6),3(地址#7,#8),4(地址#9,# 10), 5 (无地址)]
$queryBuilder = $this->em->createQueryBuilder('u');
$queryBuilder
->leftJoin('u.addresses', 'a', \Doctrine\ORM\Query\Expr\Join::WITH, $queryBuilder->expr()->in('a.id', ':ids'))
->setParameter('ids', [1, 4, 7])
;
-
左连接 +
addSelect('a')
==> [1(地址#1),2(地址#4),3(地址#7),4(无地址),5(无地址)]
$queryBuilder = $this->em->createQueryBuilder('u');
$queryBuilder
->leftJoin('u.addresses', 'a', \Doctrine\ORM\Query\Expr\Join::WITH, $queryBuilder->expr()->in('a.id', ':ids'))
->addSelect('a')
->setParameter('ids', [1, 4, 7])
;
-
INNER JOIN + SIZE() 条件
==> [1(地址#1),2(地址#4),3(地址#7)](但不是用户5)
$queryBuilder = $this->em->createQueryBuilder('u');
$queryBuilder
->innerJoin('u.addresses', 'a', \Doctrine\ORM\Query\Expr\Join::WITH, $queryBuilder->expr()->in('a.id', ':ids'))
->orWhere($queryBuilder->expr()->eq('SIZE(u.addresses)', 0))
->setParameter('ids', [1, 4, 7])
;
是否可以通过单个查询实现,如果可以,如何实现?
【问题讨论】:
【参考方案1】:如果要选择没有任何地址的用户,则需要执行LEFT JOIN
并测试检索到的值是否为NULL
(如here 所述)。因此,如果您希望结合这两个条件,您需要像这样构建您的查询:
$qb = $this->createQueryBuilder('u')
->leftJoin('u.addresses', 'a')
->addSelect('a') // If you wish to retrieve the address at the same time
->where('a.id IS NULL OR a.id IN (:ids)')
->setParameter('ids', $ids);
根据您的用例,您甚至可以像这样编写 where 条件:'a.id IS NULL OR a.city = :cityName'
以按城市名称进行过滤,避免事先检索 addresses
条目的 ID。
使用上面的查询生成器,Doctrine 会生成如下所示的 SQL 查询:
SELECT ... FROM users u0_
LEFT JOIN addresses a1_ ON u0_.id = a1_.user_id
WHERE a1_.id IS NULL OR a1_.id IN (1, 4, 7)
【讨论】:
不知道怎么可能我什至没有想到...谢谢:) 关于:cityName
,我的用例是一个例子,实际用例不涉及按名称但来自许多不同参数的过滤器。还是谢谢!以上是关于学说选择innerJoined实体或没有关联的实体的主要内容,如果未能解决你的问题,请参考以下文章