CakePHP combining Recursive and Group

坚强是说给别人听的谎言 提交于 2019-12-11 07:26:26

问题


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.

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

回答1:


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. 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.

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

    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) 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.

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



来源:https://stackoverflow.com/questions/7590402/cakephp-combining-recursive-and-group

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!