3

I am going nuts trying to find a good solution, either using the set::extract() or something. I want to add a GROUP BY within my containable:

$params = array(
    'conditions'=>array(
        'Project.id'=>$ProjectId
    ),
    'contain'=>array(
        //Gets the User Who owns the Project
        'User'=>$user,
        'Bid'=>array(
            //The User Who owns the Bid
            'User'=>$user
        ),
        'ProjectType',
        'Os',
        'Comment'=>array(
            'To'=>$user,
            'From'=>$user,
            'group'=>"Comment.from_id"
        ),
    ),
);
//debug($params);
return $this->find('first',$params);

I do not want to hack to get around this issue - is there an easier way to do this?

Daniel Causebrook
  • 469
  • 1
  • 8
  • 20
numerical25
  • 10,524
  • 36
  • 130
  • 209
  • How exactly do you want to retrieve the Comment's? I don't see an aggregate column, so I don't know why you would need to group in the first place (also you really can't group on just that one field in your case). So what is it you're trying to do? – Elte Hupkes Nov 01 '11 at 23:49
  • For those who wish to use group by in order to perform a count, there is an efficient workaround here: https://stackoverflow.com/q/2308087/1836940 – Daniel Causebrook Apr 09 '19 at 16:57

2 Answers2

5

For anyone else that stumbles on this via Google, it looks as though GROUP BY conditions for containable queries aren't supported in Cake 1 or 2, so a manual join would be required if grouping is a must.

Dom Stubbs
  • 1,198
  • 7
  • 15
  • 1
    ["Seems to be resolved with 3.0"](https://github.com/cakephp/cakephp/issues/1759#issuecomment-31795002) – Gabe Jul 24 '14 at 04:22
-1

You can do conditions within contained items:

'contain'=>array(
    'Comment'=>array(
        'To'=>$user,
        'From'=>$user,
        'conditions'=>array(
            'group'=>"Comment.from_id"
        ),
     ),
)

http://book.cakephp.org/view/1323/Containable

Shaz MJ
  • 1,785
  • 1
  • 12
  • 25