Browse Source

Reduce code duplication.

ADmad 10 years ago
parent
commit
be4eeef6de

+ 12 - 2
src/ORM/Association.php

@@ -436,8 +436,7 @@ abstract class Association
             $this->_propertyName = $name;
         }
         if ($name === null && !$this->_propertyName) {
-            list(, $name) = pluginSplit($this->_name);
-            $this->_propertyName = Inflector::underscore($name);
+            $this->_propertyName = $this->_propertyName();
             if (in_array($this->_propertyName, $this->_sourceTable->schema()->columns())) {
                 $msg = 'Association property name "%s" clashes with field of same name of table "%s".' .
                     "\n" . 'You should explicitly specify the "propertyName" option.';
@@ -451,6 +450,17 @@ abstract class Association
     }
 
     /**
+     * Returns default property name based on association name.
+     *
+     * @return string
+     */
+    protected function _propertyName()
+    {
+        list(, $name) = pluginSplit($this->_name);
+        return Inflector::underscore($name);
+    }
+
+    /**
      * Sets the strategy name to be used to fetch associated records. Keep in mind
      * that some association types might not implement but a default strategy,
      * rendering any changes to this setting void.

+ 4 - 13
src/ORM/Association/BelongsTo.php

@@ -72,23 +72,14 @@ class BelongsTo extends Association
     }
 
     /**
-     * Sets the property name that should be filled with data from the target table
-     * in the source table record.
-     * If no arguments are passed, currently configured type is returned.
+     * Returns default property name based on association name.
      *
-     * @param string|null $name The property name, use null to read the current property.
      * @return string
      */
-    public function property($name = null)
+    protected function _propertyName()
     {
-        if ($name !== null) {
-            return parent::property($name);
-        }
-        if ($name === null && !$this->_propertyName) {
-            list(, $name) = pluginSplit($this->_name);
-            $this->_propertyName = Inflector::underscore(Inflector::singularize($name));
-        }
-        return $this->_propertyName;
+        list(, $name) = pluginSplit($this->_name);
+        return Inflector::underscore(Inflector::singularize($name));
     }
 
     /**

+ 4 - 13
src/ORM/Association/HasOne.php

@@ -57,23 +57,14 @@ class HasOne extends Association
     }
 
     /**
-     * Sets the property name that should be filled with data from the target table
-     * in the source table record.
-     * If no arguments are passed, currently configured type is returned.
+     * Returns default property name based on association name.
      *
-     * @param string|null $name The name of the property. Pass null to read the current value.
      * @return string
      */
-    public function property($name = null)
+    protected function _propertyName()
     {
-        if ($name !== null) {
-            return parent::property($name);
-        }
-        if ($name === null && !$this->_propertyName) {
-            list(, $name) = pluginSplit($this->_name);
-            $this->_propertyName = Inflector::underscore(Inflector::singularize($name));
-        }
-        return $this->_propertyName;
+        list(, $name) = pluginSplit($this->_name);
+        return Inflector::underscore(Inflector::singularize($name));
     }
 
     /**

+ 9 - 3
tests/TestCase/ORM/AssociationCollectionTest.php

@@ -91,7 +91,11 @@ class AssociationCollectionTest extends TestCase
      */
     public function testGetByProperty()
     {
-        $belongsTo = new BelongsTo('Users', []);
+        $table = $this->getMock('Cake\ORM\Table', ['table'], [[]]);
+        $table->schema([]);
+        $belongsTo = new BelongsTo('Users', [
+            'sourceTable' => $table
+        ]);
         $this->assertEquals('user', $belongsTo->property());
         $this->associations->add('Users', $belongsTo);
         $this->assertNull($this->associations->get('user'));
@@ -186,7 +190,8 @@ class AssociationCollectionTest extends TestCase
      */
     public function testSaveParents()
     {
-        $table = $this->getMock('Cake\ORM\Table', [], [[]]);
+        $table = $this->getMock('Cake\ORM\Table', ['table'], [[]]);
+        $table->schema([]);
         $mockOne = $this->getMock(
             'Cake\ORM\Association\BelongsTo',
             ['saveAssociated'],
@@ -235,7 +240,8 @@ class AssociationCollectionTest extends TestCase
      */
     public function testSaveParentsFiltered()
     {
-        $table = $this->getMock('Cake\ORM\Table', [], [[]]);
+        $table = $this->getMock('Cake\ORM\Table', ['table'], [[]]);
+        $table->schema([]);
         $mockOne = $this->getMock(
             'Cake\ORM\Association\BelongsTo',
             ['saveAssociated'],