diff --git a/src/ORM/Associations.php b/src/ORM/Associations.php index 52f230fb5ba..fbd464b72ce 100644 --- a/src/ORM/Associations.php +++ b/src/ORM/Associations.php @@ -36,11 +36,15 @@ class Associations { /** * Add an association to the collection * + * If the alias added contains a `.` the part preceding the `.` will be dropped. + * This makes using plugins simpler as the Plugin.Class syntax is frequently used. + * * @param string $alias The association alias * @param Association The association to add. - * @return void + * @return Association The association object being added. */ public function add($alias, Association $association) { + list($plugin, $alias) = pluginSplit($alias); return $this->_items[strtolower($alias)] = $association; } diff --git a/tests/TestCase/ORM/AssociationsTest.php b/tests/TestCase/ORM/AssociationsTest.php index cc126df0ccf..64efd205d57 100644 --- a/tests/TestCase/ORM/AssociationsTest.php +++ b/tests/TestCase/ORM/AssociationsTest.php @@ -62,6 +62,21 @@ public function testAddHasRemoveAndGet() { $this->assertNull($this->associations->get('Users')); } +/** + * Test associations with plugin names. + * + * @return void + */ + public function testAddHasRemoveGetWithPlugin() { + $this->assertFalse($this->associations->has('Photos.Photos')); + $this->assertFalse($this->associations->has('Photos')); + + $belongsTo = new BelongsTo([]); + $this->assertSame($belongsTo, $this->associations->add('Photos.Photos', $belongsTo)); + $this->assertTrue($this->associations->has('Photos')); + $this->assertFalse($this->associations->has('Photos.Photos')); + } + /** * Test keys() *