cakephp - 如何更改分页条件?
在同一控制器的几个地方,我有这样的代码:
$publications = $this->paginate('Publication', $conditions);
...其中$conditions是条件数组,选择哪些字段以及如何选择(在mysql中使用LIKE)。
他们每个人都有不同的“Order by”声明。
我怎样才能实现这一点,并定义sql的不同“order by”部分?
更新(2011-03-11):这是我定义分页的部分方式:
$this->paginate = array(
'limit' => 1,
'fields' => array('`Publication`.*, PublicationNumeration.*, Collection.*, Publisher.*, Publication.title as PublicationTitle'),
'joins' => array( array('table' => 'publication_numerations', 'alias' => 'PublicationNumeration', 'type' => 'LEFT', 'conditions' => array( 'Publication.id = PublicationNumeration.publication_id' ) ) )
, 'order' => array('PublicationTitle desc')
);
$publications = $this->paginate('Publication', $conditions);
$this->set(compact('publications', 'urlArgs'));
带有“limit”的部分有效,但“order”不起作用,并且根本不显示在调试模式下。
on several places in same controller i have code like:
$publications = $this->paginate('Publication', $conditions);
...where $conditions is array of conditions, which fields to select and how (using LIKE in mysql).
every one of them have different "Order by" statement.
how can i achieve that, and define different "order by" part of sql?
UPDATE (2011-03-11): this is part how i defined pagination:
$this->paginate = array(
'limit' => 1,
'fields' => array('`Publication`.*, PublicationNumeration.*, Collection.*, Publisher.*, Publication.title as PublicationTitle'),
'joins' => array( array('table' => 'publication_numerations', 'alias' => 'PublicationNumeration', 'type' => 'LEFT', 'conditions' => array( 'Publication.id = PublicationNumeration.publication_id' ) ) )
, 'order' => array('PublicationTitle desc')
);
$publications = $this->paginate('Publication', $conditions);
$this->set(compact('publications', 'urlArgs'));
part with 'limit' works, but 'order' does not works, and does not show in debug mode at all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
每次在每个不同的操作中实现分页功能时,您都可以覆盖分页功能。例如:
在您的情况下,您可能希望重用
$conditions
数组,但为每个不同的实现更改'order'
的值。进一步参考:分页文档
You can override the pagination function for each time that you implement it in each different action. For example:
In your case, you'd probably want to reuse your
$conditions
array but alter the value of'order'
for each different implementation.Further Reference: Pagination doc
我使用这个想法来创建每页项目的自定义选择。
我的默认分页:
我将其添加到我的控制器中以将限制更改为 25(还为 5 和 10 添加 1,只是为了看看它会如何并相应地更改限制值):
然后,在我的主视图 (index.ctp) 中我在数据列表的底部添加了此内容:
...在显示屏底部显示以下文本:显示:5 10 25
单击时,这些数字会链接到控制器操作,从而重置限制(并使用新设置的限制刷新页面)。
也许有更好的方法来实现相同的功能,因为我必须为每个选项创建一个视图:list5.ctp、list10.ctp 和 list25.ctp。无论如何,它为用户提供了选择他们想要在页面上看到多少数据的机会。评论?
....noob cakePHP 用户...
I used this idea to create a custom selection of items per page.
My default pagination:
I added this to my controller to change the limit to 25 (also added 1 for 5 and 10 just to see how it would go and changed the limit value accordingly):
Then, in my main view (index.ctp) I added this at the bottom of the data listing:
...which shows the following text at the bottom of the display: Show: 5 10 25
The numbers, when clicked, are linked to the controller action and thus resets the limit (and refreshes the page with the new limit set).
Perhaps there is a better way to achieve the same functionality as I had to create a view for each option: list5.ctp, list10.ctp and list25.ctp. Anyway, it provides the user a chance to choose how much data they want to see on a page. Comments?
....noob cakePHP user...
试试这个:
echo $this->Paginator->link('5',array('limit' => '5'))
echo $this->Paginator->link('10',array('limit' => '10'));
echo $this->Paginator->link('10',array('limit' => '25'));
Try this:
echo $this->Paginator->link('5',array('limit' => '5'))
echo $this->Paginator->link('10',array('limit' => '10'));
echo $this->Paginator->link('10',array('limit' => '25'));