2009-12-05 184 views
3

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

$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); 

回答

0

MySQL的限制条款表示支持音量和偏移,每页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 

通用

+0

我希望为使用PAGINATE功能CakePHP中 – 2009-12-05 00:35:29

4

实现模型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(.....'); 
} 

还检查了PAGINATE功能:蛋糕/库/控制器/ Controller.php这样

2

这看起来应该能够完成而不诉诸使用自定义分页。

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

汽车的hasMany(或hasOne)CarImage
CarImage属于关联租车

那么你应该能够用得到这个确切的数据如下:

<?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

+0

怎么样当查询调用存储过程返回一个结果这是不是模型的东西吗?即。返回与模型不同的表结构。你如何分类这种情况?我有这个函数在模型'公共函数GetSummary(){ $ sql =“CALL main_report()”; return $ this-> query($ sql); }' – indago 2013-11-16 04:43:29

0

mysql限制子句支持音量和偏移量,所以每页有10个结果....

1

在Cake 3中,您可以使用函数find()的deafult paginator。

按照例如:

$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']);