CakePHP 2.x GROUP BY in Containable

CakePHP 2.x GROUP BY in Containable,php,cakephp,lamp,cakephp-2.x,Php,Cakephp,Lamp,Cakephp 2.x,我正在疯狂地试图找到一个好的解决方案,或者使用set::extract()或其他方法。我想在我的Containeble中添加一个GROUP BY: $params = array( 'conditions'=>array( 'Project.id'=>$ProjectId ), 'contain'=>array( //Gets the User Who owns the Project 'User'=>

我正在疯狂地试图找到一个好的解决方案,或者使用
set::extract()
或其他方法。我想在我的Containeble中添加一个GROUP BY:

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

我不想绕过这个问题-有更简单的方法吗?

您可以在包含的项目中执行以下操作:

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

对于其他通过谷歌偶然发现这一点的人来说,它看起来像是可包含查询的
分组条件,因此如果必须分组,则需要手动加入。

分组不是一个条件。您希望如何准确检索评论的内容?我看不到聚合列,所以我不知道为什么首先需要分组(而且在您的案例中,您真的不能只在一个字段上分组)。那么,您想做什么呢?对于那些希望使用group by来执行计数的人,这里有一个有效的解决方法: