Refactroing in TranslateBehavior. Some code was moved to protected

methods, simplified the coditions.
This commit is contained in:
Val Bancer 2017-07-07 01:20:09 +02:00
parent d9f2117436
commit 3440615323

View file

@ -147,7 +147,8 @@ class TranslateBehavior extends ModelBehavior {
$this->_joinTable = $joinTable;
$this->_runtimeModel = $RuntimeModel;
if (is_string($query['fields']) && $query['fields'] === "COUNT(*) AS {$db->name('count')}") {
if (is_string($query['fields'])) {
if ($query['fields'] === "COUNT(*) AS {$db->name('count')}") {
$query['fields'] = "COUNT(DISTINCT({$db->name($Model->escapeField())})) {$db->alias}count";
$query['joins'][] = array(
'type' => $this->runtime[$Model->alias]['joinType'],
@ -165,10 +166,26 @@ class TranslateBehavior extends ModelBehavior {
}
unset($this->_joinTable, $this->_runtimeModel);
return $query;
} elseif (is_string($query['fields'])) {
} else {
$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(
$this->settings[$Model->alias],
$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) {
foreach ($addFields as $_f => $field) {
$aliasField = is_numeric($_f) ? $field : $_f;
foreach (array($aliasField, $Model->alias . '.' . $aliasField) as $_field) {
$key = array_search($_field, (array)$query['fields']);
if ($key !== false) {
unset($query['fields'][$key]);
}
@ -207,8 +233,6 @@ class TranslateBehavior extends ModelBehavior {
$query = $this->_addJoin($Model, $query, $field, $aliasField, $locale);
}
}
$this->runtime[$Model->alias]['beforeFind'] = $addFields;
unset($this->_joinTable, $this->_runtimeModel);
return $query;
}