Fixing issue in DboSource where COUNT() was hardcoded, omitting any other aggregate functions. Replaced with a regexp that accepts only letters. Test case added. Fixes #878

This commit is contained in:
mark_story 2010-07-15 23:17:38 -04:00
parent 32ea6d24cf
commit 4c27c24a72
2 changed files with 24 additions and 2 deletions

View file

@ -1216,10 +1216,10 @@ class DboSource extends DataSource {
} elseif (!empty($model->hasMany) && $model->recursive > -1) {
$assocFields = $this->fields($model, $model->alias, array("{$model->alias}.{$model->primaryKey}"));
$passedFields = $this->fields($model, $model->alias, $queryData['fields']);
if (count($passedFields) === 1) {
$match = strpos($passedFields[0], $assocFields[0]);
$match1 = strpos($passedFields[0], 'COUNT(');
$match1 = (bool)preg_match('/^[a-z]+\(/i', $passedFields[0]);
if ($match === false && $match1 === false) {
$queryData['fields'] = array_merge($passedFields, $assocFields);
} else {

View file

@ -2044,6 +2044,28 @@ class DboSourceTest extends CakeTestCase {
unset($this->Model->hasMany['TestModel6']['fields']);
}
/**
* test generateAssociationQuery with a hasMany and an aggregate function.
*
* @return void
*/
function testGenerateAssociationQueryHasManyAndAggregateFunction() {
$this->Model =& new TestModel5();
$this->Model->schema();
$this->_buildRelatedModels($this->Model);
$binding = array('type' => 'hasMany', 'model' => 'TestModel6');
$queryData = array('fields' => array('MIN(TestModel5.test_model4_id)'));
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
$this->Model->recursive = 0;
$result = $this->testDb->generateAssociationQuery($this->Model, $null, $params['type'], $params['assoc'], $params['assocData'], $queryData, false, $resultSet);
$this->assertPattern('/^SELECT\s+MIN\(`TestModel5`\.`test_model4_id`\)\s+FROM/', $result);
}
/**
* testGenerateAssociationQueryHasAndBelongsToMany method
*