Doctrine QueryBuilder 带有子查询的复杂非表查询



我有一个SQL查询,我想用symfony/Doctrine2 QueryBuilder构建。

select     
(SELECT group_concat(DISTINCT sender)       
    FROM transactions) as senders,
(SELECT group_concat(DISTINCT receiver) 
    FROM transactions) as receivers, 
(SELECT group_concat(DISTINCT message_type) 
    FROM transactions) as types

如上面的描述所示,主查询没有表

有没有办法在查询构建器中实现这一点,而无需编写如图所示的 SQL 命令?

根据:

与构建普通查询的方式相同,构建 QueryBuilder 对象,只需提供正确的方法名称即可。下面是如何构建 QueryBuilder 对象的示例:

//$em instanceof EntityManager
// example1: creating a QueryBuilder instance
$qb = $em->createQueryBuilder();`

http://doctrine-orm.readthedocs.io/en/latest/reference/query-builder.html#constructing-a-new-querybuilder-object

但这需要根查询映射到实体。此外,查询构建器没有为子查询提供任何真正的支持,但无论如何,有一些关于如何实现的示例:

// build root query
$query = Doctrine_Query::create()
  ->from('Movie m')
  ->where('name = ?', 'Prometheus')
;
// build subquery
$subquery = $query->createSubquery()
  ->from('SeenMovie sm')
  ->where('m.name = sm.name')
;
// nest subquery and execute
$query->where('EXISTS (' . $subquery->getDql() . ')')->execute();

借用自: https://www.philipphoffmann.de/2012/08/29/a-bulletproof-pattern-for-creating-doctrine-subqueries-of-any-complexity/

相关内容

  • 没有找到相关文章