diff --git a/src/Shell/Task/ModelTask.php b/src/Shell/Task/ModelTask.php index cae9d6480..a7723e16a 100644 --- a/src/Shell/Task/ModelTask.php +++ b/src/Shell/Task/ModelTask.php @@ -115,6 +115,7 @@ public function bake($name) $validation = $this->getValidation($model, $associations); $rulesChecker = $this->getRules($model, $associations); $behaviors = $this->getBehaviors($model); + $connection = $this->connection; $data = compact( 'associations', @@ -124,7 +125,8 @@ public function bake($name) 'fields', 'validation', 'rulesChecker', - 'behaviors' + 'behaviors', + 'connection' ); $this->bakeTable($model, $data); $this->bakeEntity($model, $data); @@ -706,6 +708,7 @@ public function bakeTable($model, array $data = []) 'validation' => [], 'rulesChecker' => [], 'behaviors' => [], + 'connection' => $this->connection, ]; $this->BakeTemplate->set($data); diff --git a/src/Template/Bake/Model/table.ctp b/src/Template/Bake/Model/table.ctp index 59e19c932..3eef8396e 100644 --- a/src/Template/Bake/Model/table.ctp +++ b/src/Template/Bake/Model/table.ctp @@ -150,4 +150,16 @@ endforeach; return $rules; } <% endif; %> +<% if ($connection != 'default'): %> + + /** + * Returns the database connection name to use by default. + * + * @return string + */ + public static function defaultConnectionName() + { + return '<%= $connection %>'; + } +<% endif; %> } diff --git a/tests/TestCase/Shell/Task/ModelTaskTest.php b/tests/TestCase/Shell/Task/ModelTaskTest.php index 0f54ea666..c7532e6ed 100644 --- a/tests/TestCase/Shell/Task/ModelTaskTest.php +++ b/tests/TestCase/Shell/Task/ModelTaskTest.php @@ -896,6 +896,7 @@ public function testBakeTableConfig() 'primaryKey' => ['id'], 'displayField' => 'title', 'behaviors' => ['Timestamp' => ''], + 'connection' => 'website', ]; $model = TableRegistry::get('BakeArticles'); $result = $this->Task->bakeTable($model, $config); diff --git a/tests/comparisons/Model/testBakeTableConfig.php b/tests/comparisons/Model/testBakeTableConfig.php index d84353490..39302c9d0 100644 --- a/tests/comparisons/Model/testBakeTableConfig.php +++ b/tests/comparisons/Model/testBakeTableConfig.php @@ -26,4 +26,14 @@ public function initialize(array $config) $this->primaryKey('id'); $this->addBehavior('Timestamp'); } + + /** + * Returns the database connection name to use by default. + * + * @return string + */ + public static function defaultConnectionName() + { + return 'website'; + } } diff --git a/tests/comparisons/Model/testBakeTableRelations.php b/tests/comparisons/Model/testBakeTableRelations.php index 14d27863b..2d9a083c1 100644 --- a/tests/comparisons/Model/testBakeTableRelations.php +++ b/tests/comparisons/Model/testBakeTableRelations.php @@ -37,4 +37,14 @@ public function initialize(array $config) 'targetForeignKey' => 'bake_tag_id' ]); } + + /** + * Returns the database connection name to use by default. + * + * @return string + */ + public static function defaultConnectionName() + { + return 'test'; + } } diff --git a/tests/comparisons/Model/testBakeTableValidation.php b/tests/comparisons/Model/testBakeTableValidation.php index c4c29c372..3189793ab 100644 --- a/tests/comparisons/Model/testBakeTableValidation.php +++ b/tests/comparisons/Model/testBakeTableValidation.php @@ -43,4 +43,14 @@ public function validationDefault(Validator $validator) return $validator; } + + /** + * Returns the database connection name to use by default. + * + * @return string + */ + public static function defaultConnectionName() + { + return 'test'; + } } diff --git a/tests/comparisons/Model/testBakeTableWithPlugin.php b/tests/comparisons/Model/testBakeTableWithPlugin.php index be6ff768b..94d83c867 100644 --- a/tests/comparisons/Model/testBakeTableWithPlugin.php +++ b/tests/comparisons/Model/testBakeTableWithPlugin.php @@ -23,4 +23,14 @@ public function initialize(array $config) { $this->primaryKey('id'); } + + /** + * Returns the database connection name to use by default. + * + * @return string + */ + public static function defaultConnectionName() + { + return 'test'; + } } diff --git a/tests/comparisons/Model/testBakeWithRules.php b/tests/comparisons/Model/testBakeWithRules.php index 4daa58753..ec5c67957 100644 --- a/tests/comparisons/Model/testBakeWithRules.php +++ b/tests/comparisons/Model/testBakeWithRules.php @@ -38,4 +38,14 @@ public function buildRules(RulesChecker $rules) $rules->add($rules->existsIn(['site_id'], 'Sites')); return $rules; } + + /** + * Returns the database connection name to use by default. + * + * @return string + */ + public static function defaultConnectionName() + { + return 'test'; + } }