Skip to content

Commit

Permalink
Bake defaultConnectionName method for tables.
Browse files Browse the repository at this point in the history
Doing this fixes baking controllers, and templates that use models that
connect to non-default sources.

Refs #63
  • Loading branch information
markstory committed Apr 1, 2015
1 parent 69a1460 commit 2b0db5a
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Shell/Task/ModelTask.php
Expand Up @@ -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',
Expand All @@ -124,7 +125,8 @@ public function bake($name)
'fields',
'validation',
'rulesChecker',
'behaviors'
'behaviors',
'connection'
);
$this->bakeTable($model, $data);
$this->bakeEntity($model, $data);
Expand Down Expand Up @@ -706,6 +708,7 @@ public function bakeTable($model, array $data = [])
'validation' => [],
'rulesChecker' => [],
'behaviors' => [],
'connection' => $this->connection,
];

$this->BakeTemplate->set($data);
Expand Down
12 changes: 12 additions & 0 deletions src/Template/Bake/Model/table.ctp
Expand Up @@ -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; %>
}
1 change: 1 addition & 0 deletions tests/TestCase/Shell/Task/ModelTaskTest.php
Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions tests/comparisons/Model/testBakeTableConfig.php
Expand Up @@ -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';
}
}
10 changes: 10 additions & 0 deletions tests/comparisons/Model/testBakeTableRelations.php
Expand Up @@ -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';
}
}
10 changes: 10 additions & 0 deletions tests/comparisons/Model/testBakeTableValidation.php
Expand Up @@ -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';
}
}
10 changes: 10 additions & 0 deletions tests/comparisons/Model/testBakeTableWithPlugin.php
Expand Up @@ -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';
}
}
10 changes: 10 additions & 0 deletions tests/comparisons/Model/testBakeWithRules.php
Expand Up @@ -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';
}
}

0 comments on commit 2b0db5a

Please sign in to comment.