Browse Source

Merge pull request #3627 from rchavik/3.0-associations-remove-all

ORM\Associations: Add removeAll() method
Mark Story 11 years ago
parent
commit
227362126b
2 changed files with 30 additions and 0 deletions
  1. 13 0
      src/ORM/Associations.php
  2. 17 0
      tests/TestCase/ORM/AssociationsTest.php

+ 13 - 0
src/ORM/Associations.php

@@ -123,6 +123,19 @@ class Associations {
 	}
 
 /**
+ * Remove all registered associations.
+ *
+ * Once removed associations will not longer be reachable
+ *
+ * @return void
+ */
+	public function removeAll() {
+		foreach ($this->_items as $alias => $object) {
+			$this->remove($alias);
+		}
+	}
+
+/**
  * Save all the associations that are parents of the given entity.
  *
  * Parent associations include any association where the given table

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

@@ -64,6 +64,23 @@ class AssociationsTest extends TestCase {
 	}
 
 /**
+ * Test removeAll method
+ *
+ * @return void
+ */
+	public function testRemoveAll() {
+		$this->assertEmpty($this->associations->keys());
+
+		$belongsTo = new BelongsTo([]);
+		$this->assertSame($belongsTo, $this->associations->add('Users', $belongsTo));
+		$belongsToMany = new BelongsToMany([]);
+		$this->assertSame($belongsToMany, $this->associations->add('Cart', $belongsToMany));
+
+		$this->associations->removeAll();
+		$this->assertEmpty($this->associations->keys());
+	}
+
+/**
  * Test getting associations by property.
  *
  * @return void