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

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

问题描述

我的 Event 模型中有一个名为 getEvents 的函数 - 您可以传递限制、startend日期、fieldsevent types 和事件 subtypes.

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 可以接受我使用的所有参数,如 joinsconditionslimit...等就像一个普通的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.

我在网上阅读其他内容,说您不应该在模型中使用分页,因为它从 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?

推荐答案

我想出如何将复杂的 find 保留在模型中而无需在控制器中再次重写它的方法是通过传递一个 $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 为真,它只返回创建的选项,然后可以在控制器的分页中使用这些选项.如果它是假的(意味着我们不想分页),它返回实际的事件结果.到目前为止,这似乎奏效了.

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() 函数中(这个方法在 Events 模型中)

In my getEvents() function (this method is in the Events model)

    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天全站免登陆