MySQL 带有限制和分页的 yii CActiveDataProvider

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4561666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:09:20  来源:igfitidea点击:

yii CActiveDataProvider with limit and pagination

mysqlactiverecordyii

提问by Savash

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:

在视图中,我使用 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...

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

Thanks.

谢谢。

采纳答案by thaddeusmt

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.

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

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.

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

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

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

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.

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

Good luck!

祝你好运!

回答by Picharnan

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:

并将 CGridView 设置为隐藏分页:

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

回答by Blizz

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:

数据提供程序的基本功能之一是您可以通过在配置中指定“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,
        )
    );