diff --git a/Cake/ORM/Associations.php b/Cake/ORM/Associations.php index 45e8af609ba..11808116016 100644 --- a/Cake/ORM/Associations.php +++ b/Cake/ORM/Associations.php @@ -1,7 +1,5 @@ _items[strtolower($alias)] = $association; } +/** + * Fetch an attached association by name. + * + * @param string $alias The association alias to get. + * @return Association|null Either the association or null. + */ public function get($alias) { + $alias = strtolower($alias); + if (isset($this->_items[$alias])) { + return $this->_items[$alias]; + } + return null; } +/** + * Check for an attached association by name. + * + * @param string $alias The association alias to get. + * @return boolean Whether or not the association exists. + */ public function has($alias) { + return isset($this->_items[strtolower($alias)]); } public function drop($alias) { diff --git a/Cake/Test/TestCase/ORM/AssociationsTest.php b/Cake/Test/TestCase/ORM/AssociationsTest.php new file mode 100644 index 00000000000..8de143dde11 --- /dev/null +++ b/Cake/Test/TestCase/ORM/AssociationsTest.php @@ -0,0 +1,57 @@ +associations = new Associations(); + } + +/** + * Test the simple add/has and get methods. + * + * @return void + */ + public function testAddHasAndGet() { + $this->assertFalse($this->associations->has('users')); + $this->assertFalse($this->associations->has('Users')); + + $this->assertNull($this->associations->get('users')); + $this->assertNull($this->associations->get('Users')); + + $belongsTo = new BelongsTo([]); + $this->assertNull($this->associations->add('Users', $belongsTo)); + $this->assertTrue($this->associations->has('users')); + $this->assertTrue($this->associations->has('Users')); + + $this->assertSame($belongsTo, $this->associations->get('users')); + $this->assertSame($belongsTo, $this->associations->get('Users')); + } + +}