2009-12-05 69 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']);