我可以对自定义查询进行分页而不覆盖默认分页吗? [英] Can I paginate a custom query without overriding the default pagination?
问题描述
在我的CakePHP(1.2)应用程序中,我有两个操作都使用分页 - 索引和搜索。
在上一个问题中,对搜索结果应用阈值分数,我需要使用 HAVING
MySQL关键字。因为CakePHP不支持这个本地,我需要下来一个自定义查询以完成这个。
我可以找到自定义查询分页的所有指南涉及覆盖 paginate()
和 paginateCount()
方法。
因为我仍然希望能够在索引上正常分页,但是我不想改变模型中的正常分页行为。
有什么办法
实际上,如果你能做到这一点,你可以做它与分页。您可以查看这里
但是为了更具体,您可以在paginate函数中添加用于查找的条件/ limit / fields / contain / order等。
不在分页中使用分组,但它应该工作:D
在你的情况下,你会有这样的:
$ this-> paginate = array(
'fields'=> array(
'Product.category_id',
'COUNT Product.hotel_id)as total'
),
'group'=> array(
'Product.category_id HAVING COUNT(Product.hotel_id)> 1')
)
);
$ data = $ this-> paginate('Product');
希望它工作,发布你的结果的评论,如果它不工作,你将有覆盖它,因为它不接受组条件...虽然我认为它会工作,因为分页是一个发现到底。 p>
编辑:
您可以尝试这样做:
覆盖分页()和paginateCount(),但调整一个条件,所以你可以知道它的分页与有或没有。类似这样:
函数分页($ conditions,$ fields,$ order,$ limit,$ page = 1,$ recursive = null,$ extra = array()){
//如果没有设置条件则返回父分页
if(empty($ conditions ['having'])
return parent :: pbinate($条件,$字段,$ order,$ limit,$ page,$ recursive,$ extra)
//如果有条件设置返回你的覆盖
//覆盖代码here b
$ b}
然后你在paginateCount有一个选择性分页。记住一旦不需要就不要设置$ conditions ['having'],或记住把它放在不影响你的find的地方);
In my CakePHP (1.2) app, I have two actions that both use pagination - index and search.
In a previous question I learnt that, in order to apply a threshold score to search results, I need to use the HAVING
MySQL keyword. Since CakePHP does not support this natively, I need to drop down to a custom query in order to accomplish this.
All the guides I can find to custom query pagination involve overriding the paginate()
and paginateCount()
methods.
Since I still want to be able to paginate normally on the index however, I don't want to change the normal pagination behaviour in the model.
Is there any way I can (ahem) have my Cake and eat it too?
Actually if you CAN do it with find you CAN do it with paginate. You can take a look here
But to be more especific you can add the conditions/limit/fields/contain/order etc that you use in find to the paginate function.
I haven't use group in the paginate but it SHOULD work :D
In your case you will have something like this:
$this->paginate = array(
'fields' => array(
'Product.category_id',
'COUNT(Product.hotel_id) as total'
),
'group' => array(
'Product.category_id HAVING COUNT(Product.hotel_id) > 1')
)
);
$data = $this->paginate('Product');
Hope it works, post a comment of your result, if it doesn't work you will have to override it, because it is not accepting the group condition... though I think it will work since pagination is a find in the end.
EDIT:
You may try to do something like this:
Override the paginate() and paginateCount() but with a tweak, sneak a condition so you can tell if its a pagination with having or not. Something like this:
function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array()){
//if no having conditions set return the parent paginate
if (empty($conditions['having'])
return parent::paginate($conditions, $fields, $order, $limit, $page, $recursive, $extra)
//if having conditions set return your override
//override code here
}
Then you do something similar in paginateCount(), that way you have a selective paginate. remember to do unset $conditions['having'] once it is not needed or remember to put it somewhere that doesn't affect your find ;)
这篇关于我可以对自定义查询进行分页而不覆盖默认分页吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!