Skip to content

Commit

Permalink
Merge pull request #241 from cakephp/issue-239
Browse files Browse the repository at this point in the history
Sets the connection name to the commands
  • Loading branch information
lorenzo authored Jun 14, 2016
2 parents f2881ab + e12a49d commit 71850bd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/ConfigurationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ trait ConfigurationTrait
*/
protected $configuration;

/**
* Connection name to be used for this request
*
* @var string
*/
protected $connection;

/**
* The console input instance
*
Expand Down Expand Up @@ -198,6 +205,7 @@ public function bootstrap(InputInterface $input, OutputInterface $output)
{
parent::bootstrap($input, $output);
$name = $this->getConnectionName($input);
$this->connection = $name;
ConnectionManager::alias($name, 'default');
$connection = ConnectionManager::get($name);

Expand Down
15 changes: 15 additions & 0 deletions src/Shell/Task/SnapshotTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ protected function getTablesToBake(Collection $collection, $options = [])
}

foreach ($tableNamesInModel as $num => $table) {
if (strpos($table, '.') !== false) {
$splitted = array_reverse(explode('.', $table, 2));

$config = ConnectionManager::config($this->connection);
$key = isset($config['schema']) ? 'schema' : 'database';
if ($config[$key] === $splitted[1]) {
$table = $splitted[0];
}
}

if (!in_array($table, $tables)) {
unset($tableNamesInModel[$num]);
}
Expand Down Expand Up @@ -214,6 +224,11 @@ protected function fetchTableName($className, $pluginName = null)
}

$namespacedClassName = App::className($className, 'Model/Table', 'Table');

if (!class_exists($namespacedClassName)) {
return $tables;
}

$reflection = new ReflectionClass($namespacedClassName);
if (!$reflection->isInstantiable()) {
return $tables;
Expand Down
12 changes: 11 additions & 1 deletion tests/test_app/App/Model/Table/NumbersTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace TestApp\Model\Table;;
namespace TestApp\Model\Table;

use Cake\ORM\Table;

Expand All @@ -19,4 +19,14 @@
*/
class NumbersTable extends Table
{
public function initialize(array $config)
{
$db = env('DB');
$schema = 'cakephp_test.';
if ($db === 'pgsql') {
$schema = '';
}

$this->table($schema . 'numbers');
}
}

0 comments on commit 71850bd

Please sign in to comment.