Cake コンソールを使用してスキーマ ファイルを生成しました。スキーマ ファイルは after() メソッドを使用して、データベースにいくつかのデフォルト レコードを作成します。
'schema create' コマンドはデフォルト データベースでは問題なく動作しますが、テスト データベースに対して同じコマンドを実行しようとして --connection パラメータを使用すると、何か奇妙なことが起こりました。test データベースの下にテーブルを作成しましたが、デフォルト データベースにレコードを挿入しようとしました。
after() メソッドと関係があるのではないかと思います。
// 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
ここに私のスキーマファイルがあります:
<?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 ...
}