开发者

CakePHP combining Recursive and Group

开发者 https://www.devze.com 2023-04-09 19:23 出处:网络
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 开发者_StackOverflow中文版the results that are Teo levels deep in the recu

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 开发者_StackOverflow中文版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
    )
);


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消