Skip to content

Commit

Permalink
Merge pull request #8241 from CakeDC/3.2-table-registry-connection-na…
Browse files Browse the repository at this point in the history
…me-7644

Allow to use the connection name with the option "connectionName" in TableLocator::get()
  • Loading branch information
markstory committed Feb 15, 2016
2 parents 1226e7f + 8d8a95a commit d475a24
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/ORM/Locator/TableLocator.php
Expand Up @@ -116,6 +116,8 @@ public function config($alias = null, $options = null)
* If no `table` option is passed, the table name will be the underscored version
* of the provided $alias.
*
* If no `connection` option is passed and passed `connectionName` string then it's used as original name to instantiate connection.
*
* If no `connection` option is passed the table's defaultConnectionName() method
* will be called to get the default connection name to use.
*
Expand Down Expand Up @@ -161,7 +163,11 @@ public function get($alias, array $options = [])
}

if (empty($options['connection'])) {
$connectionName = $options['className']::defaultConnectionName();
if (!empty($options['connectionName'])) {
$connectionName = $options['connectionName'];
} else {
$connectionName = $options['className']::defaultConnectionName();
}
$options['connection'] = ConnectionManager::get($connectionName);
}

Expand Down
2 changes: 2 additions & 0 deletions src/ORM/TableRegistry.php
Expand Up @@ -100,6 +100,8 @@ public static function config($alias = null, $options = null)
/**
* Get a table instance from the registry.
*
* See options specification in {@link TableLocator::get()}.
*
* @param string $alias The alias name you want to get.
* @param array $options The options you want to build the table with.
* @return \Cake\ORM\Table
Expand Down
15 changes: 15 additions & 0 deletions tests/TestCase/ORM/Locator/TableLocatorTest.php
Expand Up @@ -221,6 +221,21 @@ public function testGetWithConfig()
$this->assertEquals('my_articles', $result->table(), 'Should use config() data.');
}

/**
* Test that get() uses config data set with config()
*
* @return void
*/
public function testGetWithConnectionName()
{
ConnectionManager::alias('test', 'testing');
$result = $this->_locator->get('Articles', [
'connectionName' => 'testing'
]);
$this->assertEquals('articles', $result->table());
$this->assertEquals('test', $result->connection()->configName());
}

/**
* Test that get() uses config data `className` set with config()
*
Expand Down

0 comments on commit d475a24

Please sign in to comment.