【问题标题】:CakePHP Won't Apply Group By ConditionCakePHP 不会按条件应用分组
【发布时间】:2012-05-24 23:05:43
【问题描述】:

我正在尝试在我的一张表上进行查找,并且我正在动态地将条件添加到从一个模型到另一个模型的 hasMany 关系中。一切正常,除了 Cake 不会将组条件应用于查询。如果我复制生成的查询并在 MySQL 中运行它并添加我的 Group By 条件,它会很好地工作。我尝试了很多方法,但无济于事,我现在的设置方式就像 Cake 文档中的 Find 页面告诉我设置一个组一样。你可以在下面找到我的做法:

$this->Project->hasMany['ProjectTime']['conditions'] = array('user_id'=>$this->Auth->user('id'), 'date_entry >= '.$firstDay.' AND date_entry <= '.$lastDay);
    $this->Project->hasMany['ProjectTime']['order'] = array('date_entry ASC');
    $this->Project->hasMany['ProjectTime']['group'] = 'ProjectTime.date_entry';
    $this->Project->hasMany['ProjectTime']['fields'] = array('ProjectTime.date_entry, ProjectTime.user_id, ProjectTime.project_id, SUM(ProjectTime.duration) AS durationTotal');        
    $result = $this->Project->find('all', array('conditions'=>array('Project.id IN (' . implode(",", $projectTimeArray) . ')')));

我已经尝试将它直接放在模型中的 hasMany 数组中 - 没有。我试过在组周围放置一个数组 - 没有。我真的很难过,所以如果有人能提供帮助,我将不胜感激。

【问题讨论】:

    标签: php mysql cakephp group-by


    【解决方案1】:

    这是构建查询的一种非常奇怪的方式:-S

    可以这样写(假设项目有很多项目时间):

    $result = $this->Project->find('all', array(
        'conditions'=>array(
            'Project.id'=>implode(",", $projectTimeArray)
        ),
        'joins'=>array(
            array(
                'table'=>'project_times',
                'alias'=>'ProjectTime',
                'type'=>'INNER',
                'conditions'=>array(
                    'ProjectTime.project_id = Project.id',
                    'ProjectTime.user_id'=>$this->Auth->user('id'),
                    'ProjectTime.date_entry >='=>$firstDay
                    'ProjectTime.date_entry <=' => $lastDay
                ),
                'order'=>array('ProjectTime.date_entry'=>'ASC'),
                'group'=>array('ProjectTime.date_entry')
            )
        )
    ));
    

    (已输入编辑器,未经测试;-)

    【讨论】:

    • 你不需要内爆,CakePHP 理解 Project.id 的数组。
    【解决方案2】:

    我知道这个答案已经晚了,但我修改了核心以允许在 hasMany 中进行分组。我不确定 CakePHP 团队为什么会做出这样的决定,如果有人能提供他们为什么这样做的见解,我将不胜感激。

    行:/lib/Cake/Model/Datasource/DboSource.php 的第 1730 行

            case 'hasMany':
                $assocData['fields'] = $this->fields($LinkModel, $association, $assocData['fields']);
                if (!empty($assocData['foreignKey'])) {
                    $assocData['fields'] = array_merge($assocData['fields'], $this->fields($LinkModel, $association, array("{$association}.{$assocData['foreignKey']}")));
                }
    
                $query = array(
                    'conditions' => $this->_mergeConditions($this->getConstraint('hasMany', $Model, $LinkModel, $association, $assocData), $assocData['conditions']),
                    'fields' => array_unique($assocData['fields']),
                    'table' => $this->fullTableName($LinkModel),
                    'alias' => $association,
                    'order' => $assocData['order'],
                    'limit' => $assocData['limit'],
                    'offset' => $assocData['offset'],
                    'group' => $assocData['group'],
                );
    

    值 $assocData['group'] 已添加到组键中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-13
      • 2020-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多