Prosty skrypt dla mechanizmu Shell cakePHP obsługujący usuwanie tabel z bazy, czyszczenie zawartości tabel i umieszczanie w nich danych “startowych”:
App::import("core", "Security");
class StartDataShell extends Shell {
function main() {
clearCache(null, "models");
clearCache(null, "persistent");
clearCache(null, "views");
$help = true;
if(isset($this->params['test'])) {
return 1;
$help = false;
}
if(isset($this->params['drop'])) {
$this->drop_tables();
$help = false;
}elseif(isset($this->params['clear'])) {
$this->clear_all();
$help = false;
}
if(isset($this->params['fill'])) {
$this->save_data($this->data());
$help = false;
}
if( $help ) {
$this->help();
}
}
function data() {
return array(
"Group" => array(
array("id"=> 1, "name"=> "admin"),
array("id"=> 2, "name"=> "operator"),
array("id"=> 3, "name"=> "pracownik"),
),
"User"=> array(
array("id"=> 1, "username"=> "admin", "password"=> Security::hash("admin" , null, true),
"email"=> "admin@example.com", "group_id"=>1),
array("id"=> 2, "username"=> "operator", "password"=> Security::hash("operator" , null, true),
"email"=> "operator@example.com", "group_id"=>2),
array("id"=> 3, "username"=> "pracownik", "password"=> Security::hash("pracownik" , null, true),
"email"=> "pracownik@example.com", "group_id"=> 3),
array("id"=> 4, "username"=> "pracownik2", "password"=> Security::hash("pracownik2" , null, true),
"email"=> "pracownik2@example.com", "group_id"=> 3),
),
// cała reszta danych demonstracyjnych/ testowych
);
}
function help(){
$this->out("StartData ");
$this->hr();
$this->out("Usage: cake start_data
");
$this->hr();
$this->out("Params:");
$this->out("\t\t-clear - clears all data in database first");
$this->out("\t\t-fill - fills database with start data");
$this->out("\t\t-drop - drops all tables in database");
}
function save_data($data) {
$this->hr();
$this->out("Fill:");
foreach($data as $model => $records) {
$ModelClass = ClassRegistry::init($model, 'model');
$ModelClass->Behaviors->disable("Acl");
$this->out("Saving data for $model model...", false);
$result = $ModelClass->saveAll($records);
if($result) {
$this->out(" ok");
}else {
$this->out(" ***error!");
var_dump($ModelClass->validationErrors);
}
}
}
function drop_tables() {
$Model = ClassRegistry::init('StartDataDummyModel', 'model');
$tables = $Model->query("SHOW TABLES");
$query = "DROP TABLE ";
foreach($tables as $table) {
$tname = each($table["TABLE_NAMES"]);
$tname = $tname["value"];
$query .= "`$tname` ,";
}
$query = trim($query, ",");
$Model->query($query);
$this->out("Tables dropped", true);
}
function clear_all() {
$this->hr();
$this->out("Clear:");
$models = Configure::listObjects("model");
foreach($models as $model) {
App::import("model", $model);
$this->out("Deleting $model: ", false);
$ModelClass = ClassRegistry::init($model, "model");
$ModelClass->Behaviors->disable("Acl");
$ModelClass->Behaviors->attach("Containable");
$ModelClass->contain = array();
$items = array();
if($ModelClass->useTable) {
$items = $ModelClass->find("all");
}
foreach($items as $item) {
$this->out($item[$model][$ModelClass->primaryKey].", ", false);
$ModelClass->delete($item[$model][$ModelClass->primaryKey]);
}
$this->out("");
}
}
}
App::import('core', "Model");
class StartDataDummyModel extends Model {
var $name = "StartDataDummyModel";
var $useTable = false;
}
Użycie:
./cake start_data -help
Nie jest to skrypt najwyższej klasy, na pewno brakuje w nim możliwości konfiguracji z jakiego połączenia z DB korzystać, również prawdopodobnie przydałoby się oddzielić metodę, w której definiowane są dane startowe. Prawdopodobnie przy wzroście ilości tabel (w okolicach 100) mogą zacząć występować problemy z zarządzaniem tymi danymi- jeśli tak będzie dam Wam znać jak sobie z nimi poradziliśmy. Poza tym napisany został on pod kątem MySQL (SHOW TABLES), więc przy innych engine’ach będzie wymagał modyfikacji.