User request

I have a custom query in my controller and I would like to implement a custom pagination that I found on cakephp.org, but their example is not like mine. Can someone please help me break this result in my opinion:

    $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);
+3
source share
5 answers

Implement paginate and paginateCount in your model:

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

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

Also check the paginate function in: cake / libs / controller / controller.php

+4
source

It seems like this should be done without resorting to using custom pagination.

In your models, you must establish the following relationships:

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);
  }
}
?>

( paginate , ): http://book.cakephp.org/view/74/Complex-Find-Conditions

+2

Cake 3 deafult find().

:

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

mysql limit clause , 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

mysql limit clause , 10 ....

0

Source: https://habr.com/ru/post/1724794/


All Articles