Skip to content
Permalink
Browse files

Refactoring table naming interaction into getTable()

  • Loading branch information...
markstory committed Apr 29, 2009
1 parent 0b40e5c commit d705e8943ab28839781483fa22974cadc3480806
Showing with 25 additions and 27 deletions.
  1. +25 −27 cake/console/libs/tasks/model.php
@@ -119,33 +119,10 @@ function __interactive() {
$associations = array('belongsTo'=> array(), 'hasOne'=> array(), 'hasMany' => array(), 'hasAndBelongsToMany'=> array());
$useDbConfig = $this->DbConfig->getConfig();
$currentModelName = $this->getName($useDbConfig);
$useTable = $this->getTable($currentModelName, $useDbConfig);
$db =& ConnectionManager::getDataSource($useDbConfig);
$useTable = Inflector::tableize($currentModelName);
$fullTableName = $db->fullTableName($useTable, false);
$tableIsGood = false;
if (array_search($useTable, $this->__tables) === false) {
$this->out('');
$this->out(sprintf(__("Given your model named '%s', Cake would expect a database table named %s", true), $currentModelName, $fullTableName));
$tableIsGood = $this->in(__('Do you want to use this table?', true), array('y','n'), 'y');
}
if (low($tableIsGood) == 'n' || low($tableIsGood) == 'no') {
$useTable = $this->in(__('What is the name of the table (enter "null" to use NO table)?', true));
}
while ($tableIsGood == false && low($useTable) != 'null') {
if (is_array($this->__tables) && !in_array($useTable, $this->__tables)) {
$fullTableName = $db->fullTableName($useTable, false);
$this->out($fullTableName . ' does not exist.');
$useTable = $this->in(__('What is the name of the table (enter "null" to use NO table)?', true));
$tableIsGood = false;
} else {
$tableIsGood = true;
}
}
$fullTableName = $db->fullTableName($useTable);
$wannaDoValidation = $this->in(__('Would you like to supply validation criteria for the fields in your model?', true), array('y','n'), 'y');
@@ -794,6 +771,29 @@ function listAll($useDbConfig = 'default', $interactive = true) {
}
}
}
/**
* Interact with the user to determine the table name of a particular model
*
* @param string $modelName Name of the model you want a table for.
* @param string $useDbConfig Name of the database config you want to get tables from.
* @return void
**/
function getTable($modelName, $useDbConfig) {
$db =& ConnectionManager::getDataSource($useDbConfig);
$useTable = Inflector::tableize($modelName);
$fullTableName = $db->fullTableName($useTable, false);
$tableIsGood = false;
if (array_search($useTable, $this->__tables) === false) {
$this->out('');
$this->out(sprintf(__("Given your model named '%s', Cake would expect a database table named '%s'", true), $modelName, $fullTableName));
$tableIsGood = $this->in(__('Do you want to use this table?', true), array('y','n'), 'y');
}
if (low($tableIsGood) == 'n' || low($tableIsGood) == 'no') {
$useTable = $this->in(__('What is the name of the table (enter "null" to use NO table)?', true));
}
return $useTable;
}
/**
* Forces the user to specify the model he wants to bake, and returns the selected model name.
*
@@ -818,13 +818,11 @@ function getName($useDbConfig) {
$enteredModel = '';
}
}
if (intval($enteredModel) > 0 && intval($enteredModel) <= count($this->_modelNames)) {
$currentModelName = $this->_modelNames[intval($enteredModel) - 1];
} else {
$currentModelName = $enteredModel;
}
return $currentModelName;
}
/**

0 comments on commit d705e89

Please sign in to comment.
You can’t perform that action at this time.