Doctrine选择innerJoined实体或没有关联的实体

时间:2018-02-08 18:23:00

标签: mysql join doctrine-orm associations

我有以下具有用户和地址的关联模型:

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 |
===================================================

我想选择地址为"城市1" :addresses.id IN(1,4,7),由于某种原因,我需要包含具有正好0地址的用户。

==> [1(地址#1),2(地址#4),3(地址#7)和5(无地址)],但不是用户4(有地址但没有匹配)。

以下是我尝试过的一些问题......

  1. INNER JOIN
  2. ==> [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. LEFT JOIN
    2. ==> [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])
      ;
      
      1. LEFT JOIN + addSelect('a')
      2. ==> [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])
        ;
        
        1. INNER JOIN + SIZE()条件
        2. ==> [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 个答案:

答案 0 :(得分: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)