CakePHP组合Recursive和Group [英] CakePHP combining Recursive and Group

查看:153
本文介绍了CakePHP组合Recursive和Group的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的递归设置为两个在我的查询,这是伟大的,并返回所有的结果,我想要,但我需要分组的结果,在递归查询中的Teo水平深。

I have recursive set to Two on my find query, this is great and returns all the results i want, but i need to group by Day on the results that are Teo levels deep in the recursive query.

这是我有: Project-> Track-> Lapse 。但是我需要按照Day的分组结果,我该如何做呢?

This is what i have: Project->Track->Lapse. But i need to group by Day on the Track results, how can i do this?

$project = $this->Project->find('all', array(
    'conditions' => array(
                'Project.id' => $id,
                'Project.user_id' => $this->Auth->user('id')
    ),
    'recursive' => 2
    )
);


推荐答案

蛋糕的ORM往往非常令人失望超越琐碎。下面是我将要做的是找到一个解决方案:

IMHO, Cake's ORM tend to be very disappointing for anything beyond the trivial. Here's is what I would do to find a solution:


  1. 首先,启用调试级别2,查询蛋糕正在生成。这将取决于您具有的关联的类型。如果它为你的 find()生成一个单独的查询,你很幸运!只需在find选项数组中添加一个'group'键,就可以了。

  1. First of all, enable debug level 2 and take a look at the queries Cake is generating. That will depend on the types of the associations you have in place. If it's generating a single query for your find(), you're lucky! Just add a 'group' key to the find options array, and it's done.

由于Cake可能产生多个查询,因此有两种可能的解决方案:

Since Cake is probably generating more than one query, there are two possible solutions:

a )尝试可容纳。如果你能够操作它的选项,迫使Cake使用正确的 JOIN 构建一个单独的查询,你就可以添加'group'选项到find。

a) Try Containable. If you are able to manipulate its options to force Cake to build a single query with the proper JOINs, you are ready to just add the 'group' options to the find. However, I have often been frustrated trying that, and resorted to the following option.

b)手动通知Cake关于 JOIN s它将必须使用为了能够分组结果。您可以先解除所有相关模型的绑定( this might有用),然后使用'joins'选项强制联接。

b) Manually inform Cake about the JOINs it will have to use in order to be able to group the results. You can do that by first unbinding all associated models (this might be useful), then using the 'joins' option to force the joins. Then you can add the 'group' option to the find.

我经常使用选项2b,不仅仅是当我要分组结果时,还要通过关联模型过滤。

I actually use option 2b quite frequently, not only when I have to group results, but also to filter by associated models.

这篇关于CakePHP组合Recursive和Group的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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