November 18, 2007
In answer to post at google group i publick this simple shell utility that allow to generate list of “cake bake …” commands based on models in your database.
Usage “cake tasklist > generated.cmd”
And after it just edit generated.cmd and run it.
<?php
class TasklistShell extends Shell {
/**
* Execution method always used for tasks
*
* @return void
*/
function main() {
$this->__interactiveAuto();
}
/**
* Handles auto baking
*
* @access private
* @return void
*/
function __interactiveAuto() {
$this->out(”);
$this->out(”);
$useTable = null;
$primaryKey = ‘id’;
$validate = array();
$associations = array(‘belongsTo’=> array(), ‘hasOne’=> array(), ‘hasMany’, ‘hasAndBelongsToMany’=> array());
$useDbConfig = ‘default’;
$this->listAll($useDbConfig);
$db =& ConnectionManager::getDataSource($useDbConfig);
$this->out(‘Models list’);
$this->hr();
foreach ($this->_modelNames as $model) {
$this->out(“cake bake model $model auto”);
}
$this->out(”);
$this->out(”);
$this->out(‘Controllers list’);
$this->hr();
foreach ($this->_modelNames as $model) {
$controller=$this->_controllerName($model);
$this->out(“cake bake controller $controller scaffold”);
}
$this->out(”);
$this->out(”);
$this->out(‘Views list’);
$this->hr();
foreach ($this->_modelNames as $model) {
$controller=$this->_controllerName($model);
$this->out(“cake bake view $controller”);
}
$this->out(”);
$this->out(”);
}
/**
* outputs the a list of possible models or controllers from database
*
* @param string $useDbConfig
* @param string $type = Models or Controllers
* @return output
*/
function listAll($useDbConfig = ‘default’) {
$db =& ConnectionManager::getDataSource($useDbConfig);
$usePrefix = empty($db->config['prefix']) ? ” : $db->config['prefix'];
if ($usePrefix) {
$tables = array();
foreach ($db->listSources() as $table) {
if (!strncmp($table, $usePrefix, strlen($usePrefix))) {
$tables[] = substr($table, strlen($usePrefix));
}
}
} else {
$tables = $db->listSources();
}
$this->__tables = $tables;
//$this->out(’Possible Models based on your current database:’);
$this->_modelNames = array();
$count = count($tables);
for ($i = 0; $i < $count; $i++) {
$this->_modelNames[] = $this->_modelName($tables[$i]);
//$this->out($i + 1 . ”. ” . $this->_modelNames[$i]);
}
}
/**
* Forces the user to specify the model he wants to bake, and returns the selected model name.
*
* @return the model name
*/
function getName($useDbConfig) {
$this->listAll($useDbConfig);
$enteredModel = ”;
while ($enteredModel == ”) {
$enteredModel = $this->in(‘Enter a number from the list above, or type in the name of another model.’);
if ($enteredModel == ” || intval($enteredModel) > count($this->_modelNames)) {
$this->out(‘Error:’);
$this->out(“The model name you supplied was empty, or the number \nyou selected was not an option. Please try again.”);
$enteredModel = ”;
}
}
if (intval($enteredModel) > 0 && intval($enteredModel) <= count($this->_modelNames)) {
$currentModelName = $this->_modelNames[intval($enteredModel) - 1];
} else {
$currentModelName = $enteredModel;
}
return $currentModelName;
}
/**
* Displays help contents
*
* @return void
*/
function help() {
$this->hr();
$this->out(“Usage: cake tasklist”);
$this->hr();
$this->out(“this shell generate list of bake commands”);
$this->out(“”);
exit();
}
}
?>