CakePHP不会按条件应用组 [英] CakePHP Won't Apply Group By Condition

查看:181
本文介绍了CakePHP不会按条件应用组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的一个表上查找,并且我正在动态地将hasMany关系的条件从一个模型添加到另一个模型。一切工作正常,除了Cake不会将组条件应用于查询。如果我复制生成的查询,并在MySQL中运行它并添加我的分组条件,它会很好地工作。我尝试了很多东西,但都无济于事,现在我拥有它的方式就像Cake文档中的查找页面设置告诉我通过设置一个组。

  $ this->项目 - > 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数组中 - 什么都没有。我曾尝试在该组周围放置一个阵列 - 什么都没有。我真的很难过,所以如果任何人都可以帮助的话,我会非常感激。

但我修改了核心以允许hasMany中的组。我不确定为什么CakePHP团队做出这样的决定,如果任何人都可以提供洞察力,为什么他们有,我将不胜感激。



行:1730年/lib/Cake/Model/Datasource/DboSource.php

  case'hasMany':
$ assocData ['fields '] = $ this->字段($ 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']),$ b $'table'=> $ this-> fullTableName($ LinkModel),
'alias'=> $ association,
'order'=> $ assocData ['order'],
'limit'=> ; $ assocData ['limit'],
'offset'=> $ assocData ['offset'],
'group'=> $ assocData ['group'],
) ;

Value $ assocData ['group']已添加到组密钥中。


I am trying to do a find on one of my tables, and I'm dynamically adding conditions to the hasMany relationship from one model to the other. Everything works fine, except, Cake will not apply the group condition to the query. If I copy the query that is generated and run it in MySQL and add my Group By condition, it works beautifully. I have tried a number of things, to no avail, and the way that I have it now is setup just like the Find page in the Cake docs tell me to setup a group by. You can find how I'm doing it below:

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

I have tried putting it directly in the hasMany array in the Model - nothing. I have tried putting an array around the group - nothing. I'm truly stumped, so if anyone can help I'd greatly appreciate it.

解决方案

I know this answer is late, but I modified the core to allow group in hasMany. I am unsure why the CakePHP team has made the decision to do so and if anyone can provide insight to why they have, I would greatly appreciate it.

Line: 1730 of /lib/Cake/Model/Datasource/DboSource.php

        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'],
            );

The Value $assocData['group'] was added to the group key.

这篇关于CakePHP不会按条件应用组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆