gpt4 book ai didi

doctrine - 如何在 Doctrine 查询 symfony 1.4 中使用联合?

转载 作者:行者123 更新时间:2023-12-01 11:02:28 25 4
gpt4 key购买 nike

我想在 mysql 中有这样的查询:

select a.x, a.y, a.foo, sum(a.bar) as bar_sum from mytable a groupby a.x union 选择 a.x, a.y, a.bar, sum(a.foo) as foo_sum from mytable a groupby是的

这种格式是真的吗?以及如何在 symfony 中将其转换为 Doctrine 查询?

非常感谢

最佳答案

我相信使用 DQL (doctrine query language) 不支持 UNION .但看起来有人能够使用讨厌的 heredoc 解决这个问题。 .

或者,您可以只在对象对等类中添加一个自定义方法,然后在 native sql 中编写查询。

-- 编辑--

使用以下示例(未经测试):

// Assuming you have the default doctrine setup, where non-object classes are
// appended with Table (your object being Foo)
class FooTable
{
public static function getFooBarUnion()
{
$sql = "select a.x, a.y, a.foo, sum(a.bar) as bar_sum from mytable a groupby a.x union select a.x, a.y, a.bar, sum(a.foo) as foo_sum from mytable a groupby a.y";

$results = Doctrine_Query::create()->query($sql);

return $results;
}
}

关于doctrine - 如何在 Doctrine 查询 symfony 1.4 中使用联合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9473327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com