【问题标题】:Set conditions for pagination设置分页条件
【发布时间】:2015-03-12 18:37:43
【问题描述】:

我正在尝试在 CakePHP 上进行自定义分页,问题是我想传递自定义条件,如下所示:

$conditions['People'] = $this->People->find('all',
    array(
        'order' => array('People.id DESC'),
        'limit' => 16, 
        'order' => 'People.id DESC'
    )
);

$people = $this->paginate('People',$conditions);

这基本上是所有涉及分页的代码,但是它不起作用,它抛出它没有找到列People...

我只是想做自定义条件的分页,也就是想提前设置条件,有没有办法呢?

【问题讨论】:

    标签: cakephp pagination


    【解决方案1】:

    你看文档了吗?

    根据文档,您应该添加如下条件:

    public function list_recipes() {
        $this->Paginator->settings = array(
            'conditions' => array('Recipe.title LIKE' => 'a%'),
            'limit' => 10
        );
        $data = $this->Paginator->paginate('Recipe');
        $this->set(compact('data'));
    }
    

    所以猜猜这​​应该可行:

    public function list_people() {
        $this->Paginator->settings = array('order' => array('People.id DESC'),'limit' => 16,'order' => 'People.id DESC');
        $data = $this->Paginator->paginate('People');
        $this->set(compact('data'));
    }
    

    【讨论】:

    • 好的,我做到了,但现在我得到“间接修改重载属性 InfoController::$Paginator 没有效果”我尝试使用文档解决它,执行“$this->Paginator =大批();”但什么都没有……
    【解决方案2】:

    好的,我解决了,这就是我所做的: 首先我声明分页,如果你想它可以是空的:

    public $paginate = array('People'=>array(
            'limit' => 6,
            'order' => 'People.id DESC'
        )); 
    

    然后,每当我想更改某些内容时,我都会这样做:

    $this->paginate['People']['limit'] = 12;
    

    【讨论】:

      【解决方案3】:

      这是我在 cake 2.x 中的做法

      首先确保在你的控制器中包含分页器组件和助手,比如

      $public $components = array(
        'Paginator'
      );
      
      $public $helpers = array(
        'Paginator'
      );
      

      然后在你的行动中

      $options = array(
          'conditions' => array(
              'Post.status' => 1
          ),
          'fields' => array(
              'Post.id',
              'Post.title',
              'Post.created'
          ),
          'order' => array(
              'Post.created' => 'DESC'
          ),
          'limit' => 10
      );
      
      $this->Paginator->settings = $options;
      $posts = $this->Paginator->paginate('Post');
      

      【讨论】:

      • 有谁知道如何在 CakePHP 4.x 中做到这一点。没有 Paginator 对象。我尝试了类似 $posts = $this->paginate('Post',$options);但它没有用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-11
      • 1970-01-01
      • 2019-10-18
      • 1970-01-01
      • 2021-06-14
      • 2020-06-28
      相关资源
      最近更新 更多