使用 Doctrine_RawSql 的子查询
Doctrine_RawSql的select字段中是否可以有子查询?
$q->select('{t.*}, {i.*}, {e.*}, {f.*}, {f2.*}');
$q->addSelect("(SELECT f.id FROM Following f WHERE f.follower_id = ? AND f.following_id = t.owner_id) AS following");
$q->addSelect("(SELECT COUNT(c.id) FROM PostComments c WHERE c.post_id = t.id) AS num_comments");
上面的例子是我已经厌倦的,但它几乎破坏了查询(它不会选择除每行主键之外的任何内容)。
有什么特殊的方法可以做到这一点还是我只是运气不好?
Is it possible to have subqueries in the select field of Doctrine_RawSql?
$q->select('{t.*}, {i.*}, {e.*}, {f.*}, {f2.*}');
$q->addSelect("(SELECT f.id FROM Following f WHERE f.follower_id = ? AND f.following_id = t.owner_id) AS following");
$q->addSelect("(SELECT COUNT(c.id) FROM PostComments c WHERE c.post_id = t.id) AS num_comments");
The above example is what I have tired, but it pretty much breaks the query (it will not select anything other than the primary keys on each row).
Is there a special way to do this or am I just out of luck?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当我有一个对于 DQL 来说有点太困难的复杂查询时,我通常只是获取 PDO 对象并手动输入查询。方法如下:
节省大量时间,并且最终结果可能也更容易理解。
When I have a complex query that's a bit too difficult for DQL I generally just grab the PDO object and enter a query manually. Here's how:
Saves alot of time and the end result is probably also a bit more understandable.
实际上,我通过修改 Doctrine_RawSql 的 parseDqlQueryPart 函数以接受参数“select_sql”(而不仅仅是“select”)并将其呈现为 DQL 来解决此问题。这允许我使用 Doctrine 而不是 PDO。
I actually fixed this problem by modifying Doctrine_RawSql's parseDqlQueryPart function into accepting a parameter, "select_sql" (instead of just "select"), and rendering it as if it were DQL. This allows me to use Doctrine instead of PDO.