【问题标题】:Conditions paginate cakephp条件分页 cakephp
【发布时间】:2020-06-28 10:46:27
【问题描述】:

我试过了

public function index($id = null)
{
    $this->paginate = [
        'contain' => ['Photos'],
    ];
   // $tareas = $this->paginate($this->Tareas);

    $this->loadModel('Categorias');

    if(isset($id)){
        $tareas2 = $this->Tareas->find()->where(['categoria_id'=>$id])->all();
        $tareas = $this->paginate($tareas2);
    }
    else
    {
        $tareas = $this->paginate($this->Tareas);
    }

        $categorias = $this->Categorias->find()->all();



    $this->set(compact('tareas','categorias'));
}

在不传递 id 时工作,但当我传递 id 时出现此错误

调用未定义的方法 Cake\ORM\ResultSet::getAlias()

【问题讨论】:

    标签: cakephp conditional-statements pagination


    【解决方案1】:

    分页器只接受表名、表实例 (\Cake\Datasource\RepositoryInterface) 和查询实例 (\Cake\Datasource\QueryInterface)。

    不要在查询对象上调用all(),这将执行查询并返回结果集,即\Cake\ORM\ResultSet 的实例,而是传递查询对象。

    另见

    【讨论】:

      【解决方案2】:

      好的,我用这个解决了它

      public function index($id = null)
      {
          if(isset($id)){
              $this->paginate = [
                  'contain' => ['Photos'],
                  'conditions' => ['categoria_id'=>$id]
              ];
          }
          else{
              $this->paginate = [
                  'contain' => ['Photos'],
              ];
          }
          $tareas = $this->paginate($this->Tareas);
      
          $this->loadModel('Categorias');
      
          $categorias = $this->Categorias->find()->all();
      
      
          $this->set(compact('tareas','categorias'));
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-22
        • 2015-06-26
        • 2011-07-10
        • 2011-09-27
        相关资源
        最近更新 更多