mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Refactroing in TranslateBehavior. Some code was moved to protected
methods, simplified the coditions.
This commit is contained in:
parent
d9f2117436
commit
3440615323
1 changed files with 48 additions and 24 deletions
|
@ -147,28 +147,45 @@ class TranslateBehavior extends ModelBehavior {
|
||||||
$this->_joinTable = $joinTable;
|
$this->_joinTable = $joinTable;
|
||||||
$this->_runtimeModel = $RuntimeModel;
|
$this->_runtimeModel = $RuntimeModel;
|
||||||
|
|
||||||
if (is_string($query['fields']) && $query['fields'] === "COUNT(*) AS {$db->name('count')}") {
|
if (is_string($query['fields'])) {
|
||||||
$query['fields'] = "COUNT(DISTINCT({$db->name($Model->escapeField())})) {$db->alias}count";
|
if ($query['fields'] === "COUNT(*) AS {$db->name('count')}") {
|
||||||
$query['joins'][] = array(
|
$query['fields'] = "COUNT(DISTINCT({$db->name($Model->escapeField())})) {$db->alias}count";
|
||||||
'type' => $this->runtime[$Model->alias]['joinType'],
|
$query['joins'][] = array(
|
||||||
'alias' => $RuntimeModel->alias,
|
'type' => $this->runtime[$Model->alias]['joinType'],
|
||||||
'table' => $joinTable,
|
'alias' => $RuntimeModel->alias,
|
||||||
'conditions' => array(
|
'table' => $joinTable,
|
||||||
$Model->escapeField() => $db->identifier($RuntimeModel->escapeField('foreign_key')),
|
'conditions' => array(
|
||||||
$RuntimeModel->escapeField('model') => $Model->name,
|
$Model->escapeField() => $db->identifier($RuntimeModel->escapeField('foreign_key')),
|
||||||
$RuntimeModel->escapeField('locale') => $locale
|
$RuntimeModel->escapeField('model') => $Model->name,
|
||||||
)
|
$RuntimeModel->escapeField('locale') => $locale
|
||||||
);
|
)
|
||||||
$conditionFields = $this->_checkConditions($Model, $query);
|
);
|
||||||
foreach ($conditionFields as $field) {
|
$conditionFields = $this->_checkConditions($Model, $query);
|
||||||
$query = $this->_addJoin($Model, $query, $field, $field, $locale);
|
foreach ($conditionFields as $field) {
|
||||||
|
$query = $this->_addJoin($Model, $query, $field, $field, $locale);
|
||||||
|
}
|
||||||
|
unset($this->_joinTable, $this->_runtimeModel);
|
||||||
|
return $query;
|
||||||
|
} else {
|
||||||
|
$query['fields'] = CakeText::tokenize($query['fields']);
|
||||||
}
|
}
|
||||||
unset($this->_joinTable, $this->_runtimeModel);
|
|
||||||
return $query;
|
|
||||||
} elseif (is_string($query['fields'])) {
|
|
||||||
$query['fields'] = CakeText::tokenize($query['fields']);
|
|
||||||
}
|
}
|
||||||
|
$addFields = $this->_getFields($Model, $query);
|
||||||
|
$this->runtime[$Model->alias]['virtualFields'] = $Model->virtualFields;
|
||||||
|
$query = $this->_addAllJoins($Model, $query, $addFields);
|
||||||
|
$this->runtime[$Model->alias]['beforeFind'] = $addFields;
|
||||||
|
unset($this->_joinTable, $this->_runtimeModel);
|
||||||
|
return $query;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets fields to be retrieved.
|
||||||
|
*
|
||||||
|
* @param Model $Model The model being worked on.
|
||||||
|
* @param array $query The query array to take fields from.
|
||||||
|
* @return array The fields.
|
||||||
|
*/
|
||||||
|
protected function _getFields(Model $Model, $query) {
|
||||||
$fields = array_merge(
|
$fields = array_merge(
|
||||||
$this->settings[$Model->alias],
|
$this->settings[$Model->alias],
|
||||||
$this->runtime[$Model->alias]['fields']
|
$this->runtime[$Model->alias]['fields']
|
||||||
|
@ -191,15 +208,24 @@ class TranslateBehavior extends ModelBehavior {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return $addFields;
|
||||||
|
}
|
||||||
|
|
||||||
$this->runtime[$Model->alias]['virtualFields'] = $Model->virtualFields;
|
/**
|
||||||
|
* Appends all necessary joins for translated fields.
|
||||||
|
*
|
||||||
|
* @param Model $Model The model being worked on.
|
||||||
|
* @param array $query The query array to append joins to.
|
||||||
|
* @param string $addFields The fields being joined.
|
||||||
|
* @return array The modified query
|
||||||
|
*/
|
||||||
|
protected function _addAllJoins(Model $Model, $query, $addFields) {
|
||||||
|
$locale = $this->_getLocale($Model);
|
||||||
if ($addFields) {
|
if ($addFields) {
|
||||||
foreach ($addFields as $_f => $field) {
|
foreach ($addFields as $_f => $field) {
|
||||||
$aliasField = is_numeric($_f) ? $field : $_f;
|
$aliasField = is_numeric($_f) ? $field : $_f;
|
||||||
|
|
||||||
foreach (array($aliasField, $Model->alias . '.' . $aliasField) as $_field) {
|
foreach (array($aliasField, $Model->alias . '.' . $aliasField) as $_field) {
|
||||||
$key = array_search($_field, (array)$query['fields']);
|
$key = array_search($_field, (array)$query['fields']);
|
||||||
|
|
||||||
if ($key !== false) {
|
if ($key !== false) {
|
||||||
unset($query['fields'][$key]);
|
unset($query['fields'][$key]);
|
||||||
}
|
}
|
||||||
|
@ -207,8 +233,6 @@ class TranslateBehavior extends ModelBehavior {
|
||||||
$query = $this->_addJoin($Model, $query, $field, $aliasField, $locale);
|
$query = $this->_addJoin($Model, $query, $field, $aliasField, $locale);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->runtime[$Model->alias]['beforeFind'] = $addFields;
|
|
||||||
unset($this->_joinTable, $this->_runtimeModel);
|
|
||||||
return $query;
|
return $query;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue