Browse Source

Allowing specifying Table when translating

Implementing translations with Table class declarations instead of the actual DB table. This gives the user the ability to specify the table and datasource used for the translation. I also added a strategy specifier to fix issues that arise when trying to search i18n'd Tables.
Patrick Conroy 11 years ago
parent
commit
2bea4c1163
1 changed files with 26 additions and 15 deletions
  1. 26 15
      src/ORM/Behavior/TranslateBehavior.php

+ 26 - 15
src/ORM/Behavior/TranslateBehavior.php

@@ -23,6 +23,7 @@ use Cake\ORM\Entity;
 use Cake\ORM\Query;
 use Cake\ORM\Table;
 use Cake\ORM\TableRegistry;
+use Cake\Utility\Inflector;
 
 /**
  * This behavior provides a way to translate dynamic data by keeping translations
@@ -65,10 +66,11 @@ class TranslateBehavior extends Behavior
         'implementedFinders' => ['translations' => 'findTranslations'],
         'implementedMethods' => ['locale' => 'locale'],
         'fields' => [],
-        'translationTable' => 'i18n',
+        'translationTable' => 'I18n',
         'defaultLocale' => '',
         'model' => '',
-        'onlyTranslated' => false
+        'onlyTranslated' => false,
+        'strategy' => 'subquery'
     ];
 
     /**
@@ -94,7 +96,8 @@ class TranslateBehavior extends Behavior
         $this->setupFieldAssociations(
             $this->_config['fields'],
             $this->_config['translationTable'],
-            $this->_config['model'] ? $this->_config['model'] : $this->_table->alias()
+            $this->_config['model'] ? $this->_config['model'] : $this->_table->alias(),
+            $this->_config['strategy']
         );
     }
 
@@ -110,16 +113,17 @@ class TranslateBehavior extends Behavior
      * @param string $model the model field value
      * @return void
      */
-    public function setupFieldAssociations($fields, $table, $model)
+    public function setupFieldAssociations($fields, $table, $model, $strategy)
     {
+        $targetAlias = Inflector::slug($table);
+        $alias = $this->_table->alias();
         $filter = $this->_config['onlyTranslated'];
+
         foreach ($fields as $field) {
-            $name = $this->_table->alias() . '_' . $field . '_translation';
-            $target = TableRegistry::get($name);
-            $target->table($table);
+            $name = $alias . '_' . $field . '_translation';
 
             $this->_table->hasOne($name, [
-                'targetTable' => $target,
+                'className' => $table,
                 'foreignKey' => 'foreign_key',
                 'joinType' => $filter ? 'INNER' : 'LEFT',
                 'conditions' => [
@@ -130,10 +134,11 @@ class TranslateBehavior extends Behavior
             ]);
         }
 
-        $this->_table->hasMany($table, [
+        $this->_table->hasMany($targetAlias, [
+            'className' => $table,
             'foreignKey' => 'foreign_key',
-            'strategy' => 'subquery',
-            'conditions' => ["$table.model" => $model],
+            'strategy' => $strategy,
+            'conditions' => ["$targetAlias.model" => $model],
             'propertyName' => '_i18n',
             'dependent' => true
         ]);
@@ -177,6 +182,7 @@ class TranslateBehavior extends Behavior
         $fields = $this->_config['fields'];
         $alias = $this->_table->alias();
         $select = $query->clause('select');
+
         $changeFilter = isset($options['filterByCurrentLocale']) &&
             $options['filterByCurrentLocale'] !== $this->_config['onlyTranslated'];
 
@@ -213,7 +219,8 @@ class TranslateBehavior extends Behavior
     {
         $locale = $entity->get('_locale') ?: $this->locale();
         $table = $this->_config['translationTable'];
-        $newOptions = [$table => ['validate' => false]];
+        $targetAlias = Inflector::slug($table);
+        $newOptions = [$targetAlias => ['validate' => false]];
         $options['associated'] = $newOptions + $options['associated'];
 
         $this->_bundleTranslatedFields($entity);
@@ -312,10 +319,12 @@ class TranslateBehavior extends Behavior
     {
         $locales = isset($options['locales']) ? $options['locales'] : [];
         $table = $this->_config['translationTable'];
+        $targetAlias = Inflector::slug($table);
+
         return $query
-            ->contain([$table => function ($q) use ($locales, $table) {
+            ->contain([$targetAlias => function ($q) use ($locales, $targetAlias) {
                 if ($locales) {
-                    $q->where(["$table.locale IN" => $locales]);
+                    $q->where(["$targetAlias.locale IN" => $locales]);
                 }
                 return $q;
             }])
@@ -458,7 +467,9 @@ class TranslateBehavior extends Behavior
      */
     protected function _findExistingTranslations($ruleSet)
     {
-        $association = $this->_table->association($this->_config['translationTable']);
+        $target = TableRegistry::get($this->_config['translationTable']);
+        $association = $this->_table->association($target->alias());
+
         $query = $association->find()
             ->select(['id', 'num' => 0])
             ->where(current($ruleSet))