【问题标题】:cakephp 3 custom sql query with pagination and sortingcakephp 3 带有分页和排序的自定义 sql 查询
【发布时间】:2018-08-15 08:29:23
【问题描述】:

我遇到了批量数据问题。我需要加入近 8 个表来获取结果,因此需要更多时间来显示结果。所以我想为此构建一个自定义 SQL 查询,我需要知道如何在 cakephp 3 中使用自定义 SQL 查询以及视图中列的排序选项进行分页。

 $sql = 'SELECT t1.f1, t2.f2, t3.f1 
         FROM table1 as t1 
         left join table2 as t2 on t1.f1 = t2.f2 
         left join table3 as t3 on t1.f3 = t3.f1'
 $results = $this->query($sql);

我需要根据这个查询对字段进行分页和排序

【问题讨论】:

    标签: pagination cakephp-3.0 columnsorting


    【解决方案1】:

    您可以将查询直接传递给控制器​​的paginate 函数,例如:

    // Build thea query as complex as required, ex:
    $query = $this->Model->find();
    
    $query = $query
        ->select(['total_comments' => $query->func()->count('Comments.id')])
        ->leftJoinWith('Comments.Users', function ($q) {
            return $q->where(['username' => 'markstory']);
        })
        ->group(['Users.id']);
    
    $this->set('data', $this->paginate($query));
    

    见:

    【讨论】:

      猜你喜欢
      • 2015-06-19
      • 1970-01-01
      • 2021-09-19
      • 2013-07-28
      • 2010-12-23
      • 1970-01-01
      • 2021-02-19
      • 1970-01-01
      • 2018-10-27
      相关资源
      最近更新 更多