使用 Doctrine_RawSql 的子查询

发布于 2024-09-27 07:43:02 字数 427 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天涯沦落人 2024-10-04 07:43:02

当我有一个对于 DQL 来说有点太困难的复杂查询时,我通常只是获取 PDO 对象并手动输入查询。方法如下:

$PDO = Doctrine_Manager::getInstance()->connection()->getDbh();
$PDO->prepare("
    //SQL Query Here
")->execute();

节省大量时间,并且最终结果可能也更容易理解。

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:

$PDO = Doctrine_Manager::getInstance()->connection()->getDbh();
$PDO->prepare("
    //SQL Query Here
")->execute();

Saves alot of time and the end result is probably also a bit more understandable.

梦明 2024-10-04 07:43:02

实际上,我通过修改 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文