Browse Source

Fix incorrectly fetched associations.

BelongsTo should not find BelongsToMany.
mark_story 12 years ago
parent
commit
ebc6152f94
2 changed files with 9 additions and 1 deletions
  1. 2 1
      src/ORM/Associations.php
  2. 7 0
      tests/TestCase/ORM/AssociationsTest.php

+ 2 - 1
src/ORM/Associations.php

@@ -104,7 +104,8 @@ class Associations {
  */
 	public function type($class) {
 		$out = array_filter($this->_items, function ($assoc) use ($class) {
-			return strpos(get_class($assoc), $class) !== false;
+			list($ns, $name) = namespaceSplit(get_class($assoc));
+			return $class === $name;
 		});
 		return array_values($out);
 	}

+ 7 - 0
tests/TestCase/ORM/AssociationsTest.php

@@ -16,6 +16,7 @@ namespace Cake\Test\TestCase\ORM;
 
 use Cake\ORM\Associations;
 use Cake\ORM\Association\BelongsTo;
+use Cake\ORM\Association\BelongsToMany;
 use Cake\ORM\Entity;
 use Cake\TestSuite\TestCase;
 
@@ -108,12 +109,18 @@ class AssociationsTest extends TestCase {
 
 /**
  * Test getting association names by type.
+ *
+ * @return void
  */
 	public function testType() {
 		$belongsTo = new BelongsTo([]);
 		$this->associations->add('Users', $belongsTo);
 
+		$belongsToMany = new BelongsToMany([]);
+		$this->associations->add('Tags', $belongsToMany);
+
 		$this->assertSame([$belongsTo], $this->associations->type('BelongsTo'));
+		$this->assertSame([$belongsToMany], $this->associations->type('BelongsToMany'));
 		$this->assertSame([], $this->associations->type('HasMany'));
 	}