joinType moved from $settings to $runtime

This commit is contained in:
Val Bancer 2017-06-26 23:02:06 +02:00
parent 0bfb19f126
commit 5b37d42f97
3 changed files with 28 additions and 28 deletions

View file

@ -61,11 +61,11 @@ class TranslateBehavior extends ModelBehavior {
* bindTranslation() method * bindTranslation() method
* *
* By default INNER joins are used to fetch translations. In order to use * By default INNER joins are used to fetch translations. In order to use
* other join types $config should contain 'join_type' key: * other join types $config should contain 'joinType' key:
* ``` * ```
* array( * array(
* 'fields' => array('field_one', 'field_two' => 'FieldAssoc', 'field_three'), * 'fields' => array('field_one', 'field_two' => 'FieldAssoc', 'field_three'),
* 'join_type' => 'LEFT', * 'joinType' => 'LEFT',
* ) * )
* ``` * ```
* In a model it may be configured this way: * In a model it may be configured this way:
@ -74,7 +74,7 @@ class TranslateBehavior extends ModelBehavior {
* 'Translate' => array( * 'Translate' => array(
* 'content', * 'content',
* 'title', * 'title',
* 'join_type' => 'LEFT', * 'joinType' => 'LEFT',
* ), * ),
* ); * );
* ``` * ```
@ -93,16 +93,16 @@ class TranslateBehavior extends ModelBehavior {
return false; return false;
} }
$this->settings[$Model->alias] = array( $this->settings[$Model->alias] = array();
$this->runtime[$Model->alias] = array(
'fields' => array(), 'fields' => array(),
'join_type' => 'INNER', 'joinType' => 'INNER',
); );
$this->runtime[$Model->alias] = array('fields' => array()); if (isset($config['joinType'])) {
$this->translateModel($Model); $this->runtime[$Model->alias]['joinType'] = $config['joinType'];
if (isset($config['join_type'])) { unset($config['joinType']);
$this->settings[$Model->alias]['join_type'] = $config['join_type'];
unset($config['join_type']);
} }
$this->translateModel($Model);
return $this->bindTranslation($Model, $config, false); return $this->bindTranslation($Model, $config, false);
} }
@ -150,7 +150,7 @@ class TranslateBehavior extends ModelBehavior {
if (is_string($query['fields']) && $query['fields'] === "COUNT(*) AS {$db->name('count')}") { if (is_string($query['fields']) && $query['fields'] === "COUNT(*) AS {$db->name('count')}") {
$query['fields'] = "COUNT(DISTINCT({$db->name($Model->escapeField())})) {$db->alias}count"; $query['fields'] = "COUNT(DISTINCT({$db->name($Model->escapeField())})) {$db->alias}count";
$query['joins'][] = array( $query['joins'][] = array(
'type' => $this->settings[$Model->alias]['join_type'], 'type' => $this->runtime[$Model->alias]['joinType'],
'alias' => $RuntimeModel->alias, 'alias' => $RuntimeModel->alias,
'table' => $joinTable, 'table' => $joinTable,
'conditions' => array( 'conditions' => array(
@ -170,7 +170,7 @@ class TranslateBehavior extends ModelBehavior {
} }
$fields = array_merge( $fields = array_merge(
$this->settings[$Model->alias]['fields'], $this->settings[$Model->alias],
$this->runtime[$Model->alias]['fields'] $this->runtime[$Model->alias]['fields']
); );
$addFields = array(); $addFields = array();
@ -226,7 +226,7 @@ class TranslateBehavior extends ModelBehavior {
return $conditionFields; return $conditionFields;
} }
foreach ($query['conditions'] as $col => $val) { foreach ($query['conditions'] as $col => $val) {
foreach ($this->settings[$Model->alias]['fields'] as $field => $assoc) { foreach ($this->settings[$Model->alias] as $field => $assoc) {
if (is_numeric($field)) { if (is_numeric($field)) {
$field = $assoc; $field = $assoc;
} }
@ -280,7 +280,7 @@ class TranslateBehavior extends ModelBehavior {
$query['fields'][] = $aliasVirtual; $query['fields'][] = $aliasVirtual;
} }
$query['joins'][] = array( $query['joins'][] = array(
'type' => $this->settings[$Model->alias]['join_type'], 'type' => $this->runtime[$Model->alias]['joinType'],
'alias' => $alias, 'alias' => $alias,
'table' => $joinTable, 'table' => $joinTable,
'conditions' => array( 'conditions' => array(
@ -400,7 +400,7 @@ class TranslateBehavior extends ModelBehavior {
if (empty($locale)) { if (empty($locale)) {
return true; return true;
} }
$fields = array_merge($this->settings[$Model->alias]['fields'], $this->runtime[$Model->alias]['fields']); $fields = array_merge($this->settings[$Model->alias], $this->runtime[$Model->alias]['fields']);
$tempData = array(); $tempData = array();
foreach ($fields as $key => $value) { foreach ($fields as $key => $value) {
@ -514,7 +514,7 @@ class TranslateBehavior extends ModelBehavior {
* @return array The fully populated data to save. * @return array The fully populated data to save.
*/ */
protected function _prepareTranslations(Model $Model, $data) { protected function _prepareTranslations(Model $Model, $data) {
$fields = array_merge($this->settings[$Model->alias]['fields'], $this->runtime[$Model->alias]['fields']); $fields = array_merge($this->settings[$Model->alias], $this->runtime[$Model->alias]['fields']);
$locales = array(); $locales = array();
foreach ($data as $key => $value) { foreach ($data as $key => $value) {
if (is_array($value)) { if (is_array($value)) {
@ -638,14 +638,14 @@ class TranslateBehavior extends ModelBehavior {
if ($reset) { if ($reset) {
$this->runtime[$Model->alias]['fields'][] = $field; $this->runtime[$Model->alias]['fields'][] = $field;
} else { } else {
$this->settings[$Model->alias]['fields'][] = $field; $this->settings[$Model->alias][] = $field;
} }
} else { } else {
if ($reset) { if ($reset) {
$this->runtime[$Model->alias]['fields'][$field] = $association; $this->runtime[$Model->alias]['fields'][$field] = $association;
$this->runtime[$Model->alias]['restoreFields'][] = $field; $this->runtime[$Model->alias]['restoreFields'][] = $field;
} else { } else {
$this->settings[$Model->alias]['fields'][$field] = $association; $this->settings[$Model->alias][$field] = $association;
} }
foreach (array('hasOne', 'hasMany', 'belongsTo', 'hasAndBelongsToMany') as $type) { foreach (array('hasOne', 'hasMany', 'belongsTo', 'hasAndBelongsToMany') as $type) {
@ -678,10 +678,10 @@ class TranslateBehavior extends ModelBehavior {
* @return void * @return void
*/ */
protected function _removeField(Model $Model, $field) { protected function _removeField(Model $Model, $field) {
if (array_key_exists($field, $this->settings[$Model->alias]['fields'])) { if (array_key_exists($field, $this->settings[$Model->alias])) {
unset($this->settings[$Model->alias]['fields'][$field]); unset($this->settings[$Model->alias][$field]);
} elseif (in_array($field, $this->settings[$Model->alias]['fields'])) { } elseif (in_array($field, $this->settings[$Model->alias])) {
$this->settings[$Model->alias]['fields'] = array_merge(array_diff($this->settings[$Model->alias]['fields'], array($field))); $this->settings[$Model->alias] = array_merge(array_diff($this->settings[$Model->alias], array($field)));
} }
if (array_key_exists($field, $this->runtime[$Model->alias]['fields'])) { if (array_key_exists($field, $this->runtime[$Model->alias]['fields'])) {
@ -701,11 +701,11 @@ class TranslateBehavior extends ModelBehavior {
* @return bool * @return bool
*/ */
public function unbindTranslation(Model $Model, $fields = null) { public function unbindTranslation(Model $Model, $fields = null) {
if (empty($fields) && empty($this->settings[$Model->alias]['fields'])) { if (empty($fields) && empty($this->settings[$Model->alias])) {
return false; return false;
} }
if (empty($fields)) { if (empty($fields)) {
return $this->unbindTranslation($Model, $this->settings[$Model->alias]['fields']); return $this->unbindTranslation($Model, $this->settings[$Model->alias]);
} }
if (is_string($fields)) { if (is_string($fields)) {

View file

@ -1399,15 +1399,15 @@ class TranslateBehaviorTest extends CakeTestCase {
$Model->unbindTranslation(); $Model->unbindTranslation();
$Model->bindTranslation(array('body', 'slug'), false); $Model->bindTranslation(array('body', 'slug'), false);
$result = $Model->Behaviors->Translate->settings['TranslatedItem']['fields']; $result = $Model->Behaviors->Translate->settings['TranslatedItem'];
$this->assertEquals(array('body', 'slug'), $result); $this->assertEquals(array('body', 'slug'), $result);
$Model->unbindTranslation(array('body')); $Model->unbindTranslation(array('body'));
$result = $Model->Behaviors->Translate->settings['TranslatedItem']['fields']; $result = $Model->Behaviors->Translate->settings['TranslatedItem'];
$this->assertNotContains('body', $result); $this->assertNotContains('body', $result);
$Model->unbindTranslation('slug'); $Model->unbindTranslation('slug');
$result = $Model->Behaviors->Translate->settings['TranslatedItem']['fields']; $result = $Model->Behaviors->Translate->settings['TranslatedItem'];
$this->assertNotContains('slug', $result); $this->assertNotContains('slug', $result);
} }

View file

@ -3253,7 +3253,7 @@ class TranslatedItemLeftJoin extends TranslatedItem {
'Translate' => array( 'Translate' => array(
'content', 'content',
'title', 'title',
'join_type' => 'LEFT', 'joinType' => 'LEFT',
) )
); );
} }