【问题标题】:Custom Query Pagination Cakephp自定义查询分页 Cakephp
【发布时间】:2010-12-23 11:02:32
【问题描述】:

我的控制器中有一个自定义查询,我想实现我在 cakephp.org 上找到的自定义查询分页,但他们的示例与我的不同。有人可以帮我在我看来分页这个结果吗:

    $cars = $this->Car->query(" select Car.id, Car.make, Car.model, Car.year, Car.description, CarImage.thumbnail
                                    from cars Car
                                    inner join car_images CarImage on Car.default_image_id = CarImage.id
                                    where Car.make like '" . $category . "'
                                    order by Car.created DESC
                                    limit 10");
    $this->set('cars', $cars);

【问题讨论】:

    标签: mysql cakephp pagination


    【解决方案1】:

    在模型中实现 paginate 和 paginateCount:

    function paginate($conditions, $fields, $order, $limit, $page, $recursive, $extra)
    {
        return $this->query('SELECT ...');
    }
    
    function paginateCount($conditions, $recursive, $extra)
    {
        return $this->query('SELECT COUNT(.....');
    }
    

    同时查看分页功能:cake/libs/controller/controller.php

    【讨论】:

      【解决方案2】:

      这看起来应该可以在不使用自定义分页的情况下完成。

      你应该在你的模型中设置以下关系:

      汽车有很多(或有一个)汽车图像
      汽车图像属于汽车

      那么您应该能够使用以下方法获得这些确切的数据:

      <?php
      class CarsController extends AppController {
        var $paginate = array('limit'=>'10',
                              'order'=>'Car.created',
                              'fields'=>array('Car.model','Car.year','Car.description',
                                              'CarImage.thumbnail'));
      
        function test() {
          // not sure where you are getting the where clause, if its from some form
          // you'll want to check look in $this->data
          $category = "somevalue";
          // set the LIKE condition
          $conditions = array('Car.make LIKE' => '%'.$category.'%');
          // apply the custom conditions to the pagination
          $this->set('cars', $this->paginate($conditions);
        }
      }
      ?>
      

      有关复杂查找条件的更多信息(可以像我在上面的示例中那样通过传入一个数组来应用于分页):http://book.cakephp.org/view/74/Complex-Find-Conditions

      【讨论】:

      • 当查询调用一个返回不是那个模型的结果集的存储过程时怎么样? IE。返回与模型不同的表结构。你怎么能对这样的场景进行分页?我在模型public function GetSummary(){ $sql="CALL main_report()"; return $this-&gt;query($sql); }中有这个功能@
      【解决方案3】:

      在 Cake 3 中,您可以使用带有函数 find() 的 deafult 分页器。

      按照示例进行:

      $query = $this->Car->find()
                      ->select(['Car.id', 'Car.make','Car.model','Car.year','Car.description','CarImage.thumbnail'])
                      ->join([
                              'table' => 'CarImage',
                              'alias' => 'CarImage',
                              'type' => 'INNER',
                              'conditions' => 'CarImage.id = Car.default_image_id,
                      ])
                      ->where(['Car.make LIKE' => '%$category%'])
                      ->order(['Car.created' => 'DESC'])
                      ->limit(50)
      
              $cars = $this->paginate($query);
      
              $this->set(compact('cars'));
              $this->set('_serialize', ['cars']);
      

      【讨论】:

        【解决方案4】:

        mysql限制子句支持volume和offset,所以每页10个结果....

        select * from table limit 0, 10
        

        第 1 页

        select * from table limit 10, 10
        

        第 2 页

        select * from table limit 20, 10
        

        第 3 页

        select * from table limit ($page-1)*$perPage, $perPage
        

        通用

        【讨论】:

        • 我希望有一些在 cakephp 中使用分页功能的东西
        【解决方案5】:

        mysql限制子句支持volume和offset,所以每页10个结果....

        【讨论】:

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