Symfony3/Doctrine2 : 使用 QueryBuilder 的 InnerJoin 子查询

Symfony3/Doctrine2 : subquery with InnerJoin using QueryBuilder

在目录中,我有产品和文章。文章是产品的变体。 在目录中,产品按类别排序,一个产品可以在目录中出现一次或多次。

我想获取目录的文章,但我的文章没有直接分配给目录,只有产品。

我想使用 Doctrine 的查询生成器构建以下 SQL:

SELECT a.code, a.productCode, a.name
FROM Article a
INNER JOIN (
    SELECT p.code
    FROM Product p
    WHERE p.catalogCode = 'MYCODE'
    GROUP BY p.code
    ORDER BY p.code ASC
) AS results ON results.productCode = a.productCode

此查询适用于 MySQL。我试图在我的实体的存储库中执行此操作,但出现错误:

public function findArticlesByCatalog($catatlogCode)
{
   return $this->getEntityManager()
        ->createQuery(
            'SELECT a.code, a.productCode, a.name
                FROM AppBundle:Article a
                INNER JOIN (
                    SELECT p.code
                    FROM AppBundle:CatalogProduct p
                    WHERE p.catalogCode = :code
                    GROUP BY p.code
                    ORDER BY p.code ASC
                ) AS results ON results.productCode = a.productCode'
        )
        ->setParameter('code', $catatlogCode)
        ->getResult();
}

错误(就在 INNER JOIN 之后):

[Semantical Error] line 0, col 81 near '(
SELECT': Error: Class '(' is not defined.

所以,我想在我的控制器中使用 Doctrine 的查询构建器来构造它。

我开始了一些事情,但我不知道要完成它...

$repository = $em->getRepository('AppBundle:Article');

$qb = $repository->createQueryBuilder('a');
$qb->select(array('a.code', 'a.productCode', 'a.name'))
    ->innerJoin(
        'AppBundle:CatalogProduct', 'p',
        'WITH',
        $qb->select('p.code')
           ->where(
              $qb->expr()->eq('p.catalogCode', ':code')
           )
           ->setParameter('code', $catCode)
           ->groupBy('p.code')
           ->orderBy('p.code', 'ASC')
    )
// ...

如何指定查询的其余部分?

AS results ON results.productCode = a.productCode'

感谢您的帮助!

我找到了正确的方法。

我重写了我的 SQL 以执行与第一个相同的操作,以便使用查询构建器轻松完成。

所以,我的第一个 SQL :

SELECT a.code, a.productCode, a.name
FROM Article a
INNER JOIN (
    SELECT p.code
    FROM Product p
    WHERE p.catalogCode = 'MYCODE'
    GROUP BY p.code
    ORDER BY p.code ASC
) AS cat_art ON cat_art.productCode = a.productCode

...与此结果相同:

SELECT DISTINCT a.code, a.productCode, a.name
FROM Article a
JOIN Product p ON a.productCode = p.code
WHERE p.code IN (
    SELECT p.code
    FROM Product p
    WHERE p.catalogCode = 'MYCODE'
    GROUP BY p.code
    ORDER BY p.code ASC
)

使用查询构建器,我们应该使用 2 个不同的查询构建器编写 2 个查询:

# It is very important here to name it "p2", not "p" because,
# in the main query, there is already "p"
$qb2 = $em->getRepository('AppBundle:CatalogProduct')->createQueryBuilder('p2');

$subQuery = $qb2->select('p2.code')
    ->where(
        $qb2->expr()->eq('p2.catalogCode', ':code')
    )
    ->groupBy('p2.code')
    ->orderBy('p2.code', 'ASC');

# main query
$qb = $em->getRepository('AppBundle:Article')->createQueryBuilder('a');

$query = $qb->select(array('DISTINCT a.code', 'a.productCode', 'a.name', 'p.code AS productCode'))
    ->join('AppBundle:CatalogProduct', 'p', 'WITH', 'a.productCode = p.code')
    ->where(
        $qb->expr()->in(
            'p.code',
            $subQuery->getDQL()
        )
    )
    // Parameter used in subquery must be set in main query.
    ->setParameter('code', $catCode)
    ->getQuery();