mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
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:
parent
32ea6d24cf
commit
4c27c24a72
2 changed files with 24 additions and 2 deletions
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue