Browse Source

Accurately derive the reference name

So that only in none-conventional/exceptional circumstances is it
necessary for developers to specify it at all
AD7six 11 years ago
parent
commit
a764fcffe7

+ 25 - 1
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
@@ -90,7 +91,7 @@ class TranslateBehavior extends Behavior
     {
         $config += [
             'defaultLocale' => I18n::defaultLocale(),
-            'referenceName' => $table->alias()
+            'referenceName' => $this->_referenceName($table)
         ];
         parent::__construct($table, $config);
     }
@@ -364,6 +365,29 @@ class TranslateBehavior extends Behavior
     }
 
     /**
+     * Determine the reference name to use for a given table
+     *
+     * The reference name is usually derived from the class name of the table object
+     * (PostsTable -> Posts), however for autotable instances it is derived from
+     * the database table the object points at - or as a last resort, the alias
+     * of the autotable instance.
+     *
+     * @param Table $table
+     * @return string
+     */
+    protected function _referenceName(Table $table)
+    {
+        $name = namespaceSplit(get_class($table));
+        $name = substr(end($name), 0, -5);
+        if (empty($name)) {
+            $name = $table->table() ?: $table->alias();
+            $name = Inflector::camelize($name);
+        }
+
+        return $name;
+    }
+
+    /**
      * Modifies the results from a table find in order to merge the translated fields
      * into each entity for a given locale.
      *

+ 34 - 4
tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php

@@ -890,18 +890,18 @@ class TranslateBehaviorTest extends TestCase
     }
 
     /**
-     * Tests the use of `model` config option.
+     * Tests the use of `referenceName` config option.
      *
      * @return void
      */
-    public function testChangingModelFieldValue()
+    public function testAutoReferenceName()
     {
         $table = TableRegistry::get('Articles');
 
         $table->hasMany('OtherComments', ['className' => 'Comments']);
         $table->OtherComments->addBehavior(
             'Translate',
-            ['fields' => ['comment'], 'referenceName' => 'Comments']
+            ['fields' => ['comment']]
         );
 
         $items = $table->OtherComments->associations();
@@ -917,7 +917,37 @@ class TranslateBehaviorTest extends TestCase
             }
         }
 
-        $this->assertTrue($found, '`model` field condition on a Translation association was not found');
+        $this->assertTrue($found, '`referenceName` field condition on a Translation association was not found');
+    }
+
+    /**
+     * Tests the use of unconventional `referenceName` config option.
+     *
+     * @return void
+     */
+    public function testChangingReferenceName()
+    {
+        $table = TableRegistry::get('Articles');
+        $table->alias('FavoritePost');
+        $table->addBehavior(
+            'Translate',
+            ['fields' => ['body'], 'referenceName' => 'Posts']
+        );
+
+        $items = $table->associations();
+        $association = $items->getByProperty('body_translation');
+        $this->assertNotEmpty($association, 'Translation association not found');
+
+        $found = false;
+        foreach ($association->conditions() as $key => $value) {
+            if (strpos($key, 'body_translation.model') !== false) {
+                $found = true;
+                $this->assertEquals('Posts', $value);
+                break;
+            }
+        }
+
+        $this->assertTrue($found, '`referenceName` field condition on a Translation association was not found');
     }
 
     /**