yii CActiveDataProvider 具有限制和分页功能

发布于 2024-10-09 09:45:35 字数 703 浏览 0 评论 0原文

我试图从表中选择几行并将其呈现在多个页面(分页)中。 这是模型中的代码:

   return new CActiveDataProvider('Downloads',
        array(
            'criteria' => array(
                'select' => 'download_id,title,thumb_ext',
                'order' => 'download_id DESC',
                'limit' => $count,
            ),
            'pagination' => array('pageSize' => 5,),
        )
    );

在视图中我使用 CGridView 显示它:

    $this->widget('zii.widgets.grid.CGridView', array(
        'dataProvider'=>$dataProvider,
        'columns' => array('download_id', 'title', 'thumb_ext'),
    ));

问题是 CActiveDataProvider 忽略条件限制并返回表的所有行...

谢谢。

I'm trying to select few rows from the table and render it in multiple pages (pagination).
This is a code in the model:

   return new CActiveDataProvider('Downloads',
        array(
            'criteria' => array(
                'select' => 'download_id,title,thumb_ext',
                'order' => 'download_id DESC',
                'limit' => $count,
            ),
            'pagination' => array('pageSize' => 5,),
        )
    );

In the view I display it using CGridView:

    $this->widget('zii.widgets.grid.CGridView', array(
        'dataProvider'=>$dataProvider,
        'columns' => array('download_id', 'title', 'thumb_ext'),
    ));

The problem is that CActiveDataProvider ignores limit of criteria and returns all rows of table...

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

浮云落日 2024-10-16 09:45:35

我不太肯定...但我认为 Yii 使用 LIMIT 子句来进行 SQL 结果分页,所以它会覆盖/替换你的 LIMIT 子句。您可以通过打开 CWebLogRoute 日志路由来检查这一点,以准确查看正在执行的 SQL。

无论如何,我不太确定这应该如何运作。您添加的 LIMIT 子句是什么?如果无论如何都要分页,为什么不让用户对所有记录进行分页呢? 解决方案可能是更改您的标准以摆脱 LIMIT 子句。

您是否尝试设置每页的结果数?不过,您已经将 pageSize 设置为 5...

另一件可以尝试的事情可能会满足您的要求,那就是检查基本 Pager 类 CPagination。使用 CLinkPager,它可能会让您比与 CGridView 一起使用的 CListPager 更有创意地进行分页。

祝你好运!

I'm not positive... but I think Yii uses the LIMIT clause to do SQL result pagination, so it will overwrite/replace your LIMIT clause. You can check this by turning on the CWebLogRoute log route to see exactly what SQL is being executed.

I'm not quite sure how this is supposed to work, anyway. What is the LIMIT clause you added for? If you are paginating anyway, why not let the user paginate through all the records? The solution is probably to change your criteria to get rid of the LIMIT clause.

Are you trying to set the number of results per page? You already have the pageSize set to 5 though...

One other thing to try that might do what you want, is to check out the base Pager class, CPagination. With CLinkPager, it might let you paginate more creatively than the CListPager you are using with the CGridView.

Good luck!

故人如初 2024-10-16 09:45:35

我遇到了同样的问题,我找到了如下解决方案:

return new CActiveDataProvider('Downloads',
    array(
        'criteria' => array(
            'select' => 'download_id,title,thumb_ext',
            'order' => 'download_id DESC',
        ),
        'pagination' => array('pageSize' => 5,),
        'totalItemCount' => $count,
    )
);

并将 CGridView 设置为分页隐藏:

$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider,
    'enablePagination' => false,
    'columns' => array('download_id', 'title', 'thumb_ext'),
));

I had the same problem and I found a solution as follows:

return new CActiveDataProvider('Downloads',
    array(
        'criteria' => array(
            'select' => 'download_id,title,thumb_ext',
            'order' => 'download_id DESC',
        ),
        'pagination' => array('pageSize' => 5,),
        'totalItemCount' => $count,
    )
);

and set CGridView to pagination is hidden:

$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider,
    'enablePagination' => false,
    'columns' => array('download_id', 'title', 'thumb_ext'),
));
离笑几人歌 2024-10-16 09:45:35

数据提供程序的基本功能之一是您可以通过在配置中指定“totalItemCount”来覆盖行数的计算。通常(我没有测试过)这也适用于 ActiveDataProvider:

return new CActiveDataProvider('Downloads',
        array(
            'criteria' => array(
                'select' => 'download_id,title,thumb_ext',
                'order' => 'download_id DESC',
            ),
            'pagination' => array('pageSize' => 5,),
            'totalItemCount' => $count,
        )
    );

One of the basic features of a data provider is that you can override the calculation of the amount of rows by specifying a "totalItemCount" in the config. Normally (I haven't tested it) this also works for the ActiveDataProvider:

return new CActiveDataProvider('Downloads',
        array(
            'criteria' => array(
                'select' => 'download_id,title,thumb_ext',
                'order' => 'download_id DESC',
            ),
            'pagination' => array('pageSize' => 5,),
            'totalItemCount' => $count,
        )
    );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文