I generated a schema file using the Cake console, and the schema file uses the after() method to create some default records in the database.
The 'schema create' command works fine for the default database but when I tried to execute the same command for my test database and used --connection parameter... something strange happened. It did create the tables under the test database but it attempted to insert the records on the default database.
I think it might have something to do with the after() method.
// Works. Creates the tables and inserts records successfully to the default
Console/cake schema create -s 1
// Breaks. Creates the tables under test but attempts to insert record in the default database
Console/cake schema create -s 1 --connection test
Here is my Schema file:
<?php
// Use this Schema for all Stage_2.0 Releases
App::uses("ClassRegistry", "Utility");
App::uses("Shoe", 'Model');
class AppSchema extends CakeSchema {
public function before($event = array()) {
// the line below always outputs 'default'... even though --connection parameter is set to 'test'
debug($this->connection);
$db = ConnectionManager::getDataSource($this->connection);
$db->cacheSources = false;
return true;
}
public function after($event = array()) {
if(isset($event['create'])){
switch($event['create']){
case "shoes":
$this->InsertSampleShoes();
break;
}
}
}
public function InsertSampleShoes(){
$shoe = ClassRegistry::init("Shoe");
$records = array(
array(
"Shoe" => array(
"name" => "Shoe 1"
)
),
array(
"Shoe" => array(
"name" => "Shoe 2"
)
)
);
$shoe->saveAll($records);
}
// ... table name, column definitions etc ...
}