diff --git a/src/ConfigurationTrait.php b/src/ConfigurationTrait.php index 80c6cbd8..a6c116e2 100644 --- a/src/ConfigurationTrait.php +++ b/src/ConfigurationTrait.php @@ -36,6 +36,13 @@ trait ConfigurationTrait */ protected $configuration; + /** + * Connection name to be used for this request + * + * @var string + */ + protected $connection; + /** * The console input instance * @@ -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); diff --git a/src/Shell/Task/SnapshotTrait.php b/src/Shell/Task/SnapshotTrait.php index 1107632d..aaaa5ab9 100644 --- a/src/Shell/Task/SnapshotTrait.php +++ b/src/Shell/Task/SnapshotTrait.php @@ -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]); } @@ -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; diff --git a/tests/test_app/App/Model/Table/NumbersTable.php b/tests/test_app/App/Model/Table/NumbersTable.php index 2c4e7e61..23be0619 100644 --- a/tests/test_app/App/Model/Table/NumbersTable.php +++ b/tests/test_app/App/Model/Table/NumbersTable.php @@ -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; @@ -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'); + } }