CakePHP:在模型中进行搜索逻辑分页 [英] CakePHP: paginating with search logic in a model

查看:84
本文介绍了CakePHP:在模型中进行搜索逻辑分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在对搜索结果进行分页时遇到麻烦.我的设置如下.

I'm having trouble paginating a search result. My setup is as follow.

我在myapp.com/searches/products处有一个搜索表单,带有视图(有搜索表单).../app/views/searches/products.ctp.我正在使用将Product模型用于此搜索查询的Searches控制器. Product具有搜索逻辑($this->find(...))的操作search().搜索结果显示在视图中表单的下方.

I have a search form at myapp.com/searches/products with view (has search form) .../app/views/searches/products.ctp. I am employing a Searches controller that uses the Product Model for this search query. Product has action search() with the search logic ($this->find(...)). The search result is displayed below the form in the view.

如何执行类似于$this->paginate()的操作,通常在控制器中完成?另外,我想知道我的设置是否有问题,尤其是包含表单和搜索结果的视图.

How do I go about performing something similar to $this->paginate(), which is normally done in the controller? Additionally, I'd like to know if there is something wrong with my setup, particularly with the view that includes both the form and the search result.

推荐答案

将搜索逻辑保留在模型中并且仍在控制器中进行分页的一种方法是:

One way to keep your search logic in the model, and still paginate in the controller is to do this:

说明:

与其返回模型的实际结果,不如返回任何/全部找到的选项,然后像往常一样进行分页.对于某些示例,例如下面的这个简单示例,它似乎有些矫kill过正,但是它为在find()中添加更多选项(例如containordergroupjoinsconditions)留下了余地. ...等.并且与胖模型,瘦控制器"的口头禅保持一致.

Instead of returning the actual results from the model, just return any/all find options, then do the pagination like you normally would. For some examples, like this simple one below, it might seem overkill, but it leaves room for adding a lot more options to your find() like contain, order, group, joins, conditions... etc etc. And stays more in-line with the "Fat Models, Skinny Controllers" mantra.

使用这样的选项设置find()也很不错,这样可以轻松在整个站点中重复使用-只需将其传递给其他选项就可以了.

It's also nice to set up your find()s with options like this so it's easily re-usable throughout your site - just pass it different options and you're good to go.

代码:

/* CONTROLLER
*/
$opts = array('paginate' => true, 'limit'=>20);
$paginateOptions = $this->Event->getEvents($opts);
$this->paginate = $paginateOptions;
$data = $this->paginate('Event');

/* MODEL
*/
public function getProducts($opts = null) {

    $params = array();

    //limit
    $params['limit'] = 50; //default
    if(!empty($opts['limit'])) $params['limit'] = $opts['limit'];

    //paginate option
    $paginate = false;
    if(isset($opts['paginate'])) {
        if($opts['paginate']) $paginate = true;
    }

    //either return the options just created (paginate)
    if($paginate) {
        return $qOpts;

    //or return the events data
    } else {
        $data = $this->find('all', $qOpts);
        return $data;
    }
}

有很多方法可以使代码更苗条/更少的代码行-但我喜欢这样编写,因此可以快速理解.

There are ways to write this a bit slimmer / less lines of code - but I like writing it like this so it's quickly understandable.

(您的总体结构似乎没有什么问题.)

(There doesn't appear to be anything wrong with your overall structure.)

这篇关于CakePHP:在模型中进行搜索逻辑分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆