symfony/doctrine 特殊查询 (MAX) 与 Doctrine_Query::create()
我正在尝试向我的应用程序添加新查询,但我有点卡住了。我也不知道该使用什么作为搜索词,所以我陷入了困境。
好吧,让我们现在开始吧:
Table: a
primary key: id
string: name
Table: b
primary key: id
foreign key: a_id
int: cluster
我想做的是检索给定对象 a 的最大簇 int。在SQL中:
SELECT MAX(b.cluster) FROM b WHERE b.a_id = ? GROUP BY b.cluster;
由于我使用的是symfony 1.4和学说1.2,我想通过向类a添加一个名为maxCluster()的方法来得到正确的结果:
class A extends BaseA{
..
public function maxCluster(){
$q = Doctrine_Query::create()->select('MAX(b.cluster)')->from('B b')->where('b.a_id = ?', $this->getId())->groupBy('b.cluster');
return Doctrine_Core::getTable('A')->execute($q);
}
}
这样做时,我收到以下错误消息:
A query with the name A/SELECT MAX(b.cluster) FROM B b WHERE b.a_id = ? GROUP BY b.cluster does not exist.
我认为解决方案应该没那么复杂,我现在只是被困住了。
感谢您的帮助, Sebastian
编辑:我得到了一个提示,所以知道 maxCluster 函数的代码如下所示:
$q = Doctrine_Query::create()->select('MAX(b.cluster) AS maxCluster')->from('B b')->where('b.a_id = ?', $this->getId())->;
$result = $q->fetchOne();
return $result->maxCluster;
现在对我来说的问题是:这是最好的方法吗?我对 fetchOne 返回图片数组感到困惑。我不希望出现这种行为,因为我选择了一个 int 而不是对象。也许任何人都可以指出此类查询的最佳实践是什么。
I am trying to add a new query to my app and I'm stuck a bit. I also don't really know what to use as search term, so I'm stuck.
Ok, let's get into it right now:
Table: a
primary key: id
string: name
Table: b
primary key: id
foreign key: a_id
int: cluster
What I want to do is retrieve the max cluster int for a given object a. In SQL:
SELECT MAX(b.cluster) FROM b WHERE b.a_id = ? GROUP BY b.cluster;
As I'm using symfony 1.4 and doctrine 1.2, I want to get it right by adding a method to Class a with the name maxCluster():
class A extends BaseA{
..
public function maxCluster(){
$q = Doctrine_Query::create()->select('MAX(b.cluster)')->from('B b')->where('b.a_id = ?', $this->getId())->groupBy('b.cluster');
return Doctrine_Core::getTable('A')->execute($q);
}
}
When doing this, I get the following error message:
A query with the name A/SELECT MAX(b.cluster) FROM B b WHERE b.a_id = ? GROUP BY b.cluster does not exist.
I think the solution should be not that complicated, I'm just stuck right now.
Thanks for any help,
Sebastian
EDIT: I got a hint, so know the code of the maxCluster-function looks like this:
$q = Doctrine_Query::create()->select('MAX(b.cluster) AS maxCluster')->from('B b')->where('b.a_id = ?', $this->getId())->;
$result = $q->fetchOne();
return $result->maxCluster;
The question for me right now is: Is this the best way? I'm confused that fetchOne returns a picture-array. I wouldn't expect such a behaviour because I select one int and not objects. Maybe anyone can point out what best pratice for such queries is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
相对于 getTable 的执行方法用于执行命名查询,请查看 学说手册,如果您想使用它们,可以找到更多信息。
或者您可以使用
return $q->execute()
返回查询结果。The execute methon relative to getTable is used to execute named queries, have a look at the doctrine manual to find more if you want to use them.
Or you can use
return $q->execute()
to return the result of the query.