CakePHP模型:COUNT(*)在Containable中 [英] CakePHP Model: COUNT(*) in Containable

查看:145
本文介绍了CakePHP模型:COUNT(*)在Containable中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CakePHP 1.3应用程式,并且非常喜欢可容纳的行为撷取资料。



让我们假设我的帖子与评论有一对多的关系。我使用Containable查询(为分页)所有帖子和所有者的评论的列表。但我只对每篇文章有多少评论感兴趣。我没有找到任何方法来实现这个查询与containable没有获取所有行的评论。我尝试:

  $ this-> paginate = array(
'fields'=>'Post.title ,Post.created',
'contains'=> array('Comment'=>'COUNT(*)AS count'),
);

会导致模型注释与模型计数错误消息无关。 p>

  $ this-> paginate = array(
'fields'=> array('Post.title,Post。创建'),
'包含'=> array('Comment'=> array('fields'=>'COUNT(*)AS count'),
);

不工作,结果集包含每个Post一个空的注释数组,除了最后一个,



我的其他猜测是

  $ this-> paginate = array(
'fields'=>'Post.title,Post.created,COUNT(Comment.id)',
' '=> array('Comment'=> array('fields'=>''),
);

但是这会导致错误,因为hasMany关系是独立查询的,因此Answer表不在Post条目的查询中。
如何计算帖子的评论数量?

解决方案

posts 表中设置一个名为 comment_count 的字段,并将此键添加到注释模型$ belongsTo Post数组:



'counterCache'=> true



每次评论发生时,相关帖子中的comment_count字段都会更新(实际上, ,而不是只是添加或删除)。



更好,因为当你为用户获取数据,它的方式更快,甚至不触摸评论表。因为你使用了Containable行为,我想你的速度和轻量级是你要找的。

I have a CakePHP 1.3 app and really enjoy the Containable behavior for fetching data.

Let's assume I have Posts in one-to-many relationship with Comments. I use Containable to query (for pagination) a list of all Posts and the belonging Comments. But I'm only interested in how many Comments each Post has. I did not found any way to achieve this query with containable without fetching all rows of Comments. I tried:

$this->paginate=array(
            'fields' => 'Post.title, Post.created',
            'contain' => array('Comment'=>'COUNT(*) AS count'),
        );

results in 'Model "Comment" is not associated with model "Count"' error message.

$this->paginate=array(
            'fields' => array('Post.title, Post.created'),
            'contain' => array('Comment'=>array('fields'=>'COUNT(*) AS count'),
        );

does not work, result set contains for each Post an empty Comment array except for the last one, where it contains the count field, but having the number of all comments not just the belonging ones.

My other guess was

$this->paginate=array(
            'fields' => 'Post.title, Post.created, COUNT(Comment.id)',
            'contain' => array('Comment'=>array('fields'=>''),
        );

but this results in an error, because hasMany relationships are queried independently, so the Answer table is not in the query for the Post entries. How can I count the number of Comments a Post has?

解决方案

Well, the best way to do this is to set up a field called comment_count in the posts table and add this key to the Comment model $belongsTo Post array:

'counterCache' => true

Every time anything will be happening with the comments, the comment_count field in the related post will be updated (actually, it recounts every time, instead of just adding or deleting).

It's better, because when you fetch data for the user, its way faster and doesn't even touch the comments table. Since you're using the Containable behaviour I guess speed and lightweight is what you're looking for.

这篇关于CakePHP模型:COUNT(*)在Containable中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆