Browse Source

Make association property() exclude plugin names.

When creating association names that include plugin names we should omit
the plugin name from the hydrated dataset. Instead of 'contacts.users',
we should just get 'users' as an example.
mark_story 12 years ago
parent
commit
176717adaa

+ 2 - 1
src/ORM/Association.php

@@ -330,7 +330,8 @@ abstract class Association {
 			$this->_propertyName = $name;
 		}
 		if ($name === null && !$this->_propertyName) {
-			$this->_propertyName = Inflector::underscore($this->_name);
+			list($plugin, $name) = pluginSplit($this->_name);
+			$this->_propertyName = Inflector::underscore($name);
 		}
 		return $this->_propertyName;
 	}

+ 2 - 1
src/ORM/Association/BelongsTo.php

@@ -82,7 +82,8 @@ class BelongsTo extends Association {
 			return parent::property($name);
 		}
 		if ($name === null && !$this->_propertyName) {
-			$this->_propertyName = Inflector::underscore(Inflector::singularize($this->_name));
+			list($plugin, $name) = pluginSplit($this->_name);
+			$this->_propertyName = Inflector::underscore(Inflector::singularize($name));
 		}
 		return $this->_propertyName;
 	}

+ 2 - 1
src/ORM/Association/HasOne.php

@@ -79,7 +79,8 @@ class HasOne extends Association {
 			return parent::property($name);
 		}
 		if ($name === null && !$this->_propertyName) {
-			$this->_propertyName = Inflector::underscore(Inflector::singularize($this->_name));
+			list($plugin, $name) = pluginSplit($this->_name);
+			$this->_propertyName = Inflector::underscore(Inflector::singularize($name));
 		}
 		return $this->_propertyName;
 	}

+ 15 - 0
tests/TestCase/ORM/Association/BelongsToManyTest.php

@@ -1294,4 +1294,19 @@ class BelongsToManyTest extends TestCase {
 		$association->save($entity);
 	}
 
+/**
+ * Test that plugin names are omitted from property()
+ *
+ * @return void
+ */
+	public function testPropertyNoPlugin() {
+		$mock = $this->getMock('Cake\ORM\Table', [], [], '', false);
+		$config = [
+			'sourceTable' => $this->article,
+			'targetTable' => $mock,
+		];
+		$association = new BelongsToMany('Contacts.Tags', $config);
+		$this->assertEquals('tags', $association->property());
+	}
+
 }

+ 15 - 0
tests/TestCase/ORM/Association/BelongsToTest.php

@@ -257,4 +257,19 @@ class BelongsToTest extends \Cake\TestSuite\TestCase {
 		$this->assertNull($entity->author_id);
 	}
 
+/**
+ * Test that plugin names are omitted from property()
+ *
+ * @return void
+ */
+	public function testPropertyNoPlugin() {
+		$mock = $this->getMock('Cake\ORM\Table', [], [], '', false);
+		$config = [
+			'sourceTable' => $this->client,
+			'targetTable' => $mock,
+		];
+		$association = new BelongsTo('Contacts.Companies', $config);
+		$this->assertEquals('company', $association->property());
+	}
+
 }

+ 16 - 0
tests/TestCase/ORM/Association/HasManyTest.php

@@ -649,4 +649,20 @@ class HasManyTest extends \Cake\TestSuite\TestCase {
 		$association = new HasMany('Articles', $config);
 		$association->save($entity);
 	}
+
+/**
+ * Test that plugin names are omitted from property()
+ *
+ * @return void
+ */
+	public function testPropertyNoPlugin() {
+		$mock = $this->getMock('Cake\ORM\Table', [], [], '', false);
+		$config = [
+			'sourceTable' => $this->author,
+			'targetTable' => $mock,
+		];
+		$association = new HasMany('Contacts.Addresses', $config);
+		$this->assertEquals('addresses', $association->property());
+	}
+
 }

+ 16 - 0
tests/TestCase/ORM/Association/HasOneTest.php

@@ -237,4 +237,20 @@ class HasOneTest extends \Cake\TestSuite\TestCase {
 
 		$this->assertSame($result, $entity);
 	}
+
+/**
+ * Test that plugin names are omitted from property()
+ *
+ * @return void
+ */
+	public function testPropertyNoPlugin() {
+		$mock = $this->getMock('Cake\ORM\Table', [], [], '', false);
+		$config = [
+			'sourceTable' => $this->user,
+			'targetTable' => $mock,
+		];
+		$association = new HasOne('Contacts.Profiles', $config);
+		$this->assertEquals('profile', $association->property());
+	}
+
 }