English 中文(简体)
CakePPHP 将按条件应用组
原标题:CakePHP Won t Apply Group By Condition

我试图在我的一张桌子上找到一个,我正在积极地为有多种关系从一种模式到另一种模式添加条件。一切都很好,除非蛋糕不会对查询适用集体条件。如果我复制生成的查询并在 MySQL 中运行并在 MySQL 中运行,并按条件添加我的集团,那它就会很好地工作。我已经尝试了许多事情,但无济于事,而现在我设置它的方式就像Cake docs 中的查找页面让我设置一个小组一样。你可以在下面找到我是如何做到的:

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

我尝试过把它直接放到模型中有很多阵列中, 什么都没有。 我尝试过在组内设置一个阵列- 什么都没有。 我实在是一无所获, 所以如果有人能帮助我, 我非常感激它。

问题回答

建立你的查询方式非常奇怪: -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团队决定这么做,如果有人能提供他们为什么这么做的洞察力,我将非常感激。

线条:1730 / lib/Cake/Model/Datasource/Dbo Source.php 1730 /lib/Cake/Model/Datasource/Dbo source.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 ],
            );

将值$assocData[组]添加到组密钥中。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签