Browse Source

Add ability to get connections without aliasing.

This will be used in the testsuite when dealing with import fixtures.
mark_story 12 years ago
parent
commit
da1934e6bd

+ 6 - 5
Cake/Database/ConnectionManager.php

@@ -131,19 +131,20 @@ class ConnectionManager {
  * defined. If you want the original unaliased connections use getOriginal()
  *
  * @param string $name The connection name.
+ * @param boolean $useAliases Set to false to not use aliased connections.
  * @return Connection A connection object.
  * @throws Cake\Error\MissingDatasourceConfigException When config data is missing.
  */
-	public static function get($name) {
-		if (empty(static::$_config[$name]) && empty(static::$_aliasMap[$name])) {
+	public static function get($name, $useAliases = true) {
+		if ($useAliases && isset(static::$_aliasMap[$name])) {
+			$name = static::$_aliasMap[$name];
+		}
+		if (empty(static::$_config[$name])) {
 			throw new Error\MissingDatasourceConfigException(['name' => $name]);
 		}
 		if (empty(static::$_registry)) {
 			static::$_registry = new ConnectionRegistry();
 		}
-		if (isset(static::$_aliasMap[$name])) {
-			$name = static::$_aliasMap[$name];
-		}
 		if (isset(static::$_registry->{$name})) {
 			return static::$_registry->{$name};
 		}

+ 15 - 0
Cake/Test/TestCase/Database/ConnectionManagerTest.php

@@ -121,6 +121,21 @@ class ConnectionManagerTest extends TestCase {
 	}
 
 /**
+ * Test loading connections without aliases
+ *
+ * @expectedException Cake\Error\Exception
+ * @expectedExceptionMessage The datasource configuration "other_name" was not found.
+ * @return void
+ */
+	public function testGetNoAlias() {
+		$config = ConnectionManager::config('test');
+		$this->skipIf(empty($config), 'No test config, skipping');
+
+		ConnectionManager::alias('test', 'other_name');
+		ConnectionManager::get('other_name', false);
+	}
+
+/**
  * Test that configured() finds configured sources.
  *
  * @return void