cakephp - 如何更改分页条件?

发布于 2024-10-21 03:41:52 字数 989 浏览 2 评论 0原文

在同一控制器的几个地方,我有这样的代码:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

演出会有结束 2024-10-28 03:41:53

每次在每个不同的操作中实现分页功能时,您都可以覆盖分页功能。例如:

function list_recipes() {
    $this->paginate = array(
        'limit' => 10,
        'order' => array(
            'Recipe.title' => 'asc'
        )
    );
    $data = $this->paginate('Recipe');
    $this->set(compact('data'));
}

在您的情况下,您可能希望重用 $conditions 数组,但为每个不同的实现更改 'order' 的值。

进一步参考:分页文档

You can override the pagination function for each time that you implement it in each different action. For example:

function list_recipes() {
    $this->paginate = array(
        'limit' => 10,
        'order' => array(
            'Recipe.title' => 'asc'
        )
    );
    $data = $this->paginate('Recipe');
    $this->set(compact('data'));
}

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

如果没有 2024-10-28 03:41:53

我使用这个想法来创建每页项目的自定义选择。
我的默认分页:

var $paginate = array( 'order' => array('User.id' => 'asc') );

我将其添加到我的控制器中以将限制更改为 25(还为 5 和 10 添加 1,只是为了看看它会如何并相应地更改限制值):

function list25() {
//fetches with different limit than default

    $this->paginate = array(
        'limit' => 25,
        'order' => array(
            'User.id' => 'asc'
        )
    );
    $data = $this->paginate('User');
    $this->set('users', $data); 
}

然后,在我的主视图 (index.ctp) 中我在数据列表的底部添加了此内容:

<span style="float:right;">
<?php echo "  <span style='vertical-align:top;font-size:12px;'>Show:</span>";
      echo $html->link(' 5', array('controller'=>'users', 'action' => 'list5'));
      echo $html->link(' 10', array('controller'=>'users', 'action' => 'list10'));
      echo $html->link(' 25', array('controller'=>'users', 'action' => 'list25'));
    ?>
</span>
<span style="float:right;margin-right:10px;"><?php  echo $paginator->numbers(); ?></span>   

...在显示屏底部显示以下文本:显示: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:

var $paginate = array( 'order' => array('User.id' => 'asc') );

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):

function list25() {
//fetches with different limit than default

    $this->paginate = array(
        'limit' => 25,
        'order' => array(
            'User.id' => 'asc'
        )
    );
    $data = $this->paginate('User');
    $this->set('users', $data); 
}

Then, in my main view (index.ctp) I added this at the bottom of the data listing:

<span style="float:right;">
<?php echo "  <span style='vertical-align:top;font-size:12px;'>Show:</span>";
      echo $html->link(' 5', array('controller'=>'users', 'action' => 'list5'));
      echo $html->link(' 10', array('controller'=>'users', 'action' => 'list10'));
      echo $html->link(' 25', array('controller'=>'users', 'action' => 'list25'));
    ?>
</span>
<span style="float:right;margin-right:10px;"><?php  echo $paginator->numbers(); ?></span>   

...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...

虫児飞 2024-10-28 03:41:53

试试这个:
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'));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文