From 872f78a1a0b2996d83940127bf594d6dc0d49604 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 12 Jan 2014 23:46:38 -0500 Subject: [PATCH] Make association names not retain plugins. Creating associations in plugins should not retain aliases like 'Photos.Photos'. Instead the plugin segment should be dropped and the association should just be called 'Photos'. This keeps behavior consistent with previous releases and avoids annoying problems when trying to contain() association names with `.` in them. --- src/ORM/Associations.php | 6 +++++- tests/TestCase/ORM/AssociationsTest.php | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) 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() *