Skip to content

Commit

Permalink
Fix issue with find(count) and translated conditions.
Browse files Browse the repository at this point in the history
Because count queries did not have joins created for the translated
fields pagination would generate invalid queries. Checking the conditions
for translated fields and adding in the correct joins solves that.
Extract what would have been duplicated code into methods.

Add a few protected properties to keep method signatures sane. The code
could be even simpler if the existing find(count) join was removed.

Fixes #2349
  • Loading branch information
markstory committed Jan 8, 2012
1 parent 049b04d commit f9bcc95
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 31 deletions.
136 changes: 105 additions & 31 deletions cake/libs/model/behaviors/translate.php
Expand Up @@ -34,6 +34,20 @@ class TranslateBehavior extends ModelBehavior {
*/
var $runtime = array();

/**
* Stores the joinTable object for generating joins.
*
* @var object
*/
var $_joinTable;

/**
* Stores the runtime model for generating joins.
*
* @var Model
*/
var $_runtimeModel;

/**
* Callback
*
Expand Down Expand Up @@ -96,6 +110,7 @@ function beforeFind(&$model, $query) {
}
$db =& ConnectionManager::getDataSource($model->useDbConfig);
$RuntimeModel =& $this->translateModel($model);

if (!empty($RuntimeModel->tablePrefix)) {
$tablePrefix = $RuntimeModel->tablePrefix;
} else {
Expand All @@ -105,8 +120,12 @@ function beforeFind(&$model, $query) {
$joinTable->tablePrefix = $tablePrefix;
$joinTable->table = $RuntimeModel->table;

if (is_string($query['fields']) && 'COUNT(*) AS '.$db->name('count') == $query['fields']) {
$this->_joinTable = $joinTable;
$this->_runtimeModel = $RuntimeModel;

if (is_string($query['fields']) && 'COUNT(*) AS ' . $db->name('count') == $query['fields']) {
$query['fields'] = 'COUNT(DISTINCT('.$db->name($model->alias . '.' . $model->primaryKey) . ')) ' . $db->alias . 'count';

$query['joins'][] = array(
'type' => 'INNER',
'alias' => $RuntimeModel->alias,
Expand All @@ -117,6 +136,11 @@ function beforeFind(&$model, $query) {
$RuntimeModel->alias.'.locale' => $locale
)
);
$conditionFields = $this->_checkConditions($model, $query);
foreach ($conditionFields as $field) {
$query = $this->_addJoin($model, $query, $field, $locale, false);
}
unset($this->_joinTable, $this->_runtimeModel);
return $query;
}
$autoFields = false;
Expand Down Expand Up @@ -150,7 +174,6 @@ function beforeFind(&$model, $query) {
if (is_array($query['fields'])) {
foreach ($fields as $key => $value) {
$field = (is_numeric($key)) ? $value : $key;

if (in_array($model->alias.'.*', $query['fields']) || $autoFields || in_array($model->alias.'.'.$field, $query['fields']) || in_array($field, $query['fields'])) {
$addFields[] = $field;
}
Expand All @@ -166,39 +189,90 @@ function beforeFind(&$model, $query) {
unset($query['fields'][$key]);
}
}
$query = $this->_addJoin($model, $query, $field, $locale, true);
}
}
$this->runtime[$model->alias]['beforeFind'] = $addFields;
unset($this->_joinTable, $this->_runtimeModel);
return $query;
}

if (is_array($locale)) {
foreach ($locale as $_locale) {
$query['fields'][] = 'I18n__'.$field.'__'.$_locale.'.content';
$query['joins'][] = array(
'type' => 'LEFT',
'alias' => 'I18n__'.$field.'__'.$_locale,
'table' => $joinTable,
'conditions' => array(
$model->alias . '.' . $model->primaryKey => $db->identifier("I18n__{$field}__{$_locale}.foreign_key"),
'I18n__'.$field.'__'.$_locale.'.model' => $model->name,
'I18n__'.$field.'__'.$_locale.'.'.$RuntimeModel->displayField => $field,
'I18n__'.$field.'__'.$_locale.'.locale' => $_locale
)
);
}
} else {
$query['fields'][] = 'I18n__'.$field.'.content';
$query['joins'][] = array(
'type' => 'INNER',
'alias' => 'I18n__'.$field,
'table' => $joinTable,
'conditions' => array(
$model->alias . '.' . $model->primaryKey => $db->identifier("I18n__{$field}.foreign_key"),
'I18n__'.$field.'.model' => $model->name,
'I18n__'.$field.'.'.$RuntimeModel->displayField => $field,
'I18n__'.$field.'.locale' => $locale
)
);
/**
* Check a query's conditions for translated fields.
* Return an array of translated fields found in the conditions.
*
* @param Model $model The model being read.
* @param array $query The query array.
* @return array The list of translated fields that are in the conditions.
*/
function _checkConditions(&$model, $query) {
$conditionFields = array();
if (empty($query['conditions']) || (!empty($query['conditions']) && !is_array($query['conditions'])) ) {
return $conditionFields;
}
foreach ($query['conditions'] as $col => $val) {
foreach ($this->settings[$model->alias] as $field => $assoc) {
if (is_numeric($field)) {
$field = $assoc;
}
if (strpos($col, $field) !== false) {
$conditionFields[] = $field;
}
}
}
$this->runtime[$model->alias]['beforeFind'] = $addFields;
return $conditionFields;
}

/**
* Appends a join for translated fields and possibly a field.
*
* @param Model $model The model being worked on.
* @param object $joinTable The jointable object.
* @param array $query The query array to append a join to.
* @param string $field The field name being joined.
* @param mixed $locale The locale(s) having joins added.
* @param boolean $addField Whether or not to add a field.
* @return array The modfied query
*/
function _addJoin(&$model, $query, $field, $locale, $addField = false) {
$db =& ConnectionManager::getDataSource($model->useDbConfig);

$RuntimeModel = $this->_runtimeModel;
$joinTable = $this->_joinTable;

if (is_array($locale)) {
foreach ($locale as $_locale) {
if ($addField) {
$query['fields'][] = 'I18n__'.$field.'__'.$_locale.'.content';
}
$query['joins'][] = array(
'type' => 'LEFT',
'alias' => 'I18n__'.$field.'__'.$_locale,
'table' => $joinTable,
'conditions' => array(
$model->alias . '.' . $model->primaryKey => $db->identifier("I18n__{$field}__{$_locale}.foreign_key"),
'I18n__'.$field.'__'.$_locale.'.model' => $model->name,
'I18n__'.$field.'__'.$_locale.'.'.$RuntimeModel->displayField => $field,
'I18n__'.$field.'__'.$_locale.'.locale' => $_locale
)
);
}
} else {
if ($addField) {
$query['fields'][] = 'I18n__'.$field.'.content';
}
$query['joins'][] = array(
'type' => 'INNER',
'alias' => 'I18n__'.$field,
'table' => $joinTable,
'conditions' => array(
$model->alias . '.' . $model->primaryKey => $db->identifier("I18n__{$field}.foreign_key"),
'I18n__'.$field.'.model' => $model->name,
'I18n__'.$field.'.'.$RuntimeModel->displayField => $field,
'I18n__'.$field.'.locale' => $locale
)
);
}
return $query;
}

Expand Down
19 changes: 19 additions & 0 deletions cake/tests/cases/libs/model/behaviors/translate.test.php
Expand Up @@ -62,6 +62,24 @@ function endTest() {
ClassRegistry::flush();
}

/**
* Test that count queries with conditions get the correct joins
*
* @return void
*/
function testCountWithConditions() {
$this->loadFixtures('Translate', 'TranslatedItem');

$Model =& new TranslatedItem();
$Model->locale = 'eng';
$result = $Model->find('count', array(
'conditions' => array(
'I18n__content.locale' => 'eng'
)
));
$this->assertEqual(3, $result);
}

/**
* testTranslateModel method
*
Expand Down Expand Up @@ -895,4 +913,5 @@ function testUnbindTranslationInfinteLoop() {

$this->assertFalse($result);
}

}

0 comments on commit f9bcc95

Please sign in to comment.