Skip to content

Commit

Permalink
Initial steps to start using virtual fields in TranslateBehavior, thi…
Browse files Browse the repository at this point in the history
…s will ease Dbo abstraction
  • Loading branch information
lorenzo committed Oct 26, 2010
1 parent 25c213e commit f94f79e
Showing 1 changed file with 21 additions and 46 deletions.
67 changes: 21 additions & 46 deletions cake/libs/model/behaviors/translate.php
Expand Up @@ -113,57 +113,34 @@ public function beforeFind(&$model, $query) {
);
return $query;
}
$autoFields = false;

if (empty($query['fields'])) {
$query['fields'] = array($model->alias.'.*');

$recursive = $model->recursive;
if (isset($query['recursive'])) {
$recursive = $query['recursive'];
}

if ($recursive >= 0) {
foreach (array('hasOne', 'belongsTo') as $type) {
foreach ($model->{$type} as $key => $value) {

if (empty($value['fields'])) {
$query['fields'][] = $key.'.*';
} else {
foreach ($value['fields'] as $field) {
$query['fields'][] = $key.'.'.$field;
}
}
}
}
}
$autoFields = true;
}
$fields = array_merge($this->settings[$model->alias], $this->runtime[$model->alias]['fields']);
$addFields = array();
if (is_array($query['fields'])) {
if (empty($query['fields'])) {
$addFields = $fields;
} else 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'])) {
if (in_array($model->alias.'.*', $query['fields']) || in_array($model->alias.'.'.$field, $query['fields']) || in_array($field, $query['fields'])) {
$addFields[] = $field;
}
}
}

if ($addFields) {
foreach ($addFields as $field) {
foreach (array($field, $model->alias.'.'.$field) as $_field) {
$key = array_search($_field, $query['fields']);

if ($key !== false) {
unset($query['fields'][$key]);
}
}
// foreach (array($field, $model->alias.'.'.$field) as $_field) {
// $key = array_search($_field, $query['fields']);
//
// if ($key !== false) {
// unset($query['fields'][$key]);
// }
// }

if (is_array($locale)) {
foreach ($locale as $_locale) {
$query['fields'][] = 'I18n__'.$field.'__'.$_locale.'.content';
$model->virtualFields['_i18n_'.$field.'_'.$_locale] = 'COALESCE('. 'I18n__'.$field.'__'.$_locale.'.content, NULL)';
$query['joins'][] = array(
'type' => 'LEFT',
'alias' => 'I18n__'.$field.'__'.$_locale,
Expand All @@ -177,7 +154,7 @@ public function beforeFind(&$model, $query) {
);
}
} else {
$query['fields'][] = 'I18n__'.$field.'.content';
$model->virtualFields['_i18n_'.$field] = 'COALESCE('. 'I18n__'.$field.'.content, NULL)';
$query['joins'][] = array(
'type' => 'LEFT',
'alias' => 'I18n__'.$field,
Expand All @@ -197,9 +174,6 @@ public function beforeFind(&$model, $query) {
}
}
}
if (is_array($query['fields'])) {
$query['fields'] = array_merge($query['fields']);
}
$this->runtime[$model->alias]['beforeFind'] = $addFields;
return $query;
}
Expand All @@ -221,10 +195,10 @@ public function afterFind(&$model, $results, $primary) {
}
$beforeFind = $this->runtime[$model->alias]['beforeFind'];

foreach ($results as $key => $row) {
$results[$key][$model->alias]['locale'] = (is_array($locale)) ? @$locale[0] : $locale;
foreach ($results as $key => &$row) {
$results[$key][$model->alias]['locale'] = (is_array($locale)) ? current($locale) : $locale;

foreach ($beforeFind as $field) {
foreach ($beforeFind as $_f => $field) {
if (is_array($locale)) {
foreach ($locale as $_locale) {
if (!isset($results[$key][$model->alias][$field]) && !empty($results[$key]['I18n__'.$field.'__'.$_locale]['content'])) {
Expand All @@ -238,11 +212,12 @@ public function afterFind(&$model, $results, $primary) {
}
} else {
$value = '';
if (!empty($results[$key]['I18n__'.$field]['content'])) {
$value = $results[$key]['I18n__'.$field]['content'];
if (!empty($row[$model->alias]['_i18n_' . $field])) {
$value = $row[$model->alias]['_i18n_' . $field];
}
$results[$key][$model->alias][$field] = $value;
unset($results[$key]['I18n__'.$field]);
$aliasField = is_numeric($_f) ? $field : $_f;
$row[$model->alias][$aliasField] = $value;
unset($row[$model->alias]['_i18n_' . $field]);
}
}
}
Expand Down

0 comments on commit f94f79e

Please sign in to comment.