Skip to content
Permalink
Browse files

Fixing issue in DboSource where COUNT() was hardcoded, omitting any o…

…ther aggregate functions. Replaced with a regexp that accepts only letters. Test case added. Fixes #878
  • Loading branch information...
markstory committed Jul 16, 2010
1 parent 32ea6d2 commit 4c27c24a72bad9b25a5986a80fd9176535aa0b8e
@@ -1216,10 +1216,10 @@ function generateAssociationQuery(&$model, &$linkModel, $type, $association = nu
} 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 {
@@ -2044,6 +2044,28 @@ function testGenerateAssociationQueryHasManyWithFields() {
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
*

0 comments on commit 4c27c24

Please sign in to comment.
You can’t perform that action at this time.