Browse Source

Merge pull request #5465 from berrygoudswaard/feature-tableregistry-delete

TableRegistry::remove()
Mark Story 11 years ago
parent
commit
018716d6fb
2 changed files with 41 additions and 0 deletions
  1. 19 0
      src/ORM/TableRegistry.php
  2. 22 0
      tests/TestCase/ORM/TableRegistryTest.php

+ 19 - 0
src/ORM/TableRegistry.php

@@ -242,4 +242,23 @@ class TableRegistry {
 		return static::$_fallbacked;
 	}
 
+/**
+ * Removes an instance from the registry.
+ *
+ * Plugin name will be trimmed off of aliases as instances
+ * stored in the registry will be without the plugin name as well.
+ *
+ * @param string $alias The alias to remove.
+ * @return void
+ */
+	public static function remove($alias) {
+		list(, $alias) = pluginSplit($alias);
+
+		unset(
+			static::$_instances[$alias],
+			static::$_config[$alias],
+			static::$_fallbacked[$alias]
+		);
+	}
+
 }

+ 22 - 0
tests/TestCase/ORM/TableRegistryTest.php

@@ -345,4 +345,26 @@ class TableRegistryTest extends TestCase {
 		$this->assertEquals($expected, TableRegistry::genericInstances());
 	}
 
+/**
+ * Tests remove an instance
+ *
+ * @return void
+ */
+	public function testRemove() {
+		Plugin::load('TestPlugin');
+
+		$pluginTable = TableRegistry::get('TestPlugin.Comments');
+		$cachedTable = TableRegistry::get('Comments');
+
+		$this->assertTrue(TableRegistry::exists('Comments'));
+		$this->assertSame($pluginTable, $cachedTable);
+
+		TableRegistry::remove('Comments');
+		$this->assertFalse(TableRegistry::exists('Comments'));
+
+		$appTable = TableRegistry::get('Comments');
+		$this->assertTrue(TableRegistry::exists('Comments'));
+		$this->assertNotSame($pluginTable, $appTable);
+	}
+
 }