Error in Nested SubQuery In DQL: Class '(' is not defined

ぃ、小莉子 提交于 2019-12-01 04:26:07

问题


I have a DQL as like below:

SELECT at, count(at.id) FROM AccountTriple at JOIN at.property p JOIN at.account ac WHERE p.create_analytics = '1' GROUP BY at.property, at.value, ac.service

As you can see, it has three joins. As 'at' and 'ac' both have large amount of data. In attempt to optimize it, I am trying to move the "p.create_analytics = '1'" checking before join to 'ac' to give it a smaller data set to join. I am trying to achieve something like this:

SELECT at, count(at.id) FROM ( SELECT at FROM AccountTriple at JOIN at.property p WHERE p.create_analytics = '1' ) JOIN at.account ac  GROUP BY at.property, at.value, ac.service

But somehow, my syntax isn't working. A error message is showing as below:

Semantical Error] line 0, col 29 near '( SELECT at FROM': Error: Class '(' is not defined.

Didn't find similar example anywhere else as well. Does anyone can help to fix this DQL query to get working? Thanks in advance.


回答1:


Use the createSubquery() function to create a subquery in Doctrine. You can then nest the subquery into your main query.

Example

// 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();

Further Reading
A Bulletproof Pattern for Creating Doctrine Subqueries of Any Complexity



来源:https://stackoverflow.com/questions/24600439/error-in-nested-subquery-in-dql-class-is-not-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!