从CakePHP中的模型中分页 [英] Paginate from within a model in CakePHP

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

问题描述

我在我的事件模型中有一个函数 getEvents - 你可以传递limit,开始结束日期,字段 code> types 和事件子类型

I have a function in my Event model called getEvents - you can pass limit, start and end dates, fields, event types, and event subtypes.

我读到 paginate 可以接受我使用的所有参数像 joins 条件限制 ...等就像一个正常的 find 可以。

I read that paginate can accept all the parameters I'm using like joins, conditions, limit...etc just like a normal find can.

当我不尝试分页时,它返回的数据很好。但是 - 我想能够通过一个 paginate 变量来告诉它,而不是这样:

It returns data just fine when I don't try to paginate. But - I'd like to be able to pass it a paginate variable to tell it instead of doing this:

$this->recursive = -1;
$data = $this->find('all', $qOptions);

可执行此操作:

$this->recursive = -1;
$data = $this->paginate($qOptions);

当我尝试,但是,它给我很多错误。我可以指定错误后,如果需要 - 现在,我想我在寻找 - 这是可以做的东西吗?如果是,如何?

When I try that, though, it gives me lots of errors. I can specify the errors later if needed - for now, I guess I'm looking for - is this something that can be done? If so, how?

有没有另一个更好的方法来做这样的事情?我花了足够的时间使这个函数做我想要的,并允许所有的选项传递...等 - 它似乎是一个浪费,如果我不能也使用它的分页。但是 - 如果它不理想,我也确定听到这一点。提前感谢。

Is there another better way to do something like this? I spent enough time making this function do just what I want, and allowing all the options passed...etc - it just seems like a waste if I can't also use it for pagination. But - if it's not ideal, I'm ok hearing that too. Thanks in advance.

编辑:

也就是说你不应该在你的模型中使用paginate,因为它从URL变量中抽取,这会破坏MVC结构的目的。这是有道理的,但这是否意味着我必须在模型和控制器中写入相同的联接/查询?

I'm reading other things online that say you shouldn't use paginate in your model, because it draws from URL variables, which defeats the MVC structure purpose. This makes sense, but does that mean I have to write the same joins/queries in both model and controller? And in every action that it's needed?

推荐答案

我想出了如何保持我的复杂在我的模型中找到,而不必在控制器中第二次重写它是通过传递 $ paginate 布尔变量。

The way I figured out how I can keep my complex find in my model without having to rewrite it a second time in the controller is by passing a $paginate boolean variable.

如果 $ paginate 为true,它只返回创建的选项,然后可以在控制器的分页中使用。如果它是假的(意味着我们不想分页),它返回实际的事件结果。到目前为止,这似乎是工作。

If $paginate is true, it returns just the options created, which can then be used in the controller's pagination. If it's false (meaning we don't want to paginate), it returns the actual event results. So far this seems to be working.

在我的 getEvents()函数)

    if($paginate) {
        return $qOpts; // Just return the options for the paginate in the controller to use

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



知道我想要分页):

Then, in my Events/Index (events controller, index action - where I know I want pagination):

$this->Event->recursive = -1; // (or set recursive = -1 in the appModel)
$opts['paginate'] = true;

$paginateOptions = $this->Event->getEvents($opts);

$this->paginate = $paginateOptions; // Set paginate options to just-returned options
$data = $this->paginate('Event'); // Get paginate results
$this->set('data', $data); // Set variable to hold paginated results in view

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

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