【问题标题】:yii CActiveDataProvider with limit and pagination带有限制和分页的 yii CActiveDataProvider
【发布时间】:2011-06-01 11:48:24
【问题描述】:

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

   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 忽略了条件的限制并返回表的所有行...

谢谢。

【问题讨论】:

  • $dataProvider 是否包含 CActiveDataProvider 对象?
  • 是的,我这里没有贴Controller的代码……

标签: mysql activerecord yii


【解决方案1】:

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

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

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

另一件可能会做你想做的事情是检查基本的 Pager 类,CPagination。使用 CLinkPager,它可能让您比使用 CGridView 的 CListPager 更有创意地进行分页。

祝你好运!

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      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'),
      ));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多