自定义查询分页 Cakephp

2021-12-21 00:00:00 mysql cakephp pagination

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

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

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

推荐答案

在你的模型中实现 paginate 和 paginateCount:

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(.....');
}

另请查看:cake/libs/controller/controller.php中的分页功能

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

相关文章