Initial refactoring of scaffold and bake.

Removing unused vars

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5917 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
phpnut 2007-10-29 03:04:08 +00:00
parent 8f12e402df
commit 6ab2b013bc
3 changed files with 44 additions and 63 deletions

View file

@ -231,28 +231,15 @@ class ViewTask extends Shell {
$this->err("The file '{$shortPath}' could not be found.\nIn order to bake a view, you'll need to first create the controller."); $this->err("The file '{$shortPath}' could not be found.\nIn order to bake a view, you'll need to first create the controller.");
exit(); exit();
} }
$controllerObj = & new $controllerClassName(); $controllerObj = & new $controllerClassName();
$controllerObj->constructClasses(); $controllerObj->constructClasses();
$modelClass = $controllerObj->modelClass;
$modelKey = $controllerObj->modelKey; $modelKey = $controllerObj->modelKey;
$modelObj =& ClassRegistry::getObject($modelKey); $modelObj =& ClassRegistry::getObject($modelKey);
$primaryKey = $modelObj->primaryKey;
$displayField = $modelObj->displayField;
$singularVar = Inflector::variable($modelClass);
$pluralVar = Inflector::variable($this->controllerName);
$singularHumanName = Inflector::humanize($modelClass);
$pluralHumanName = Inflector::humanize($this->controllerName);
$fields = array_keys($modelObj->schema());
$foreignKeys = $modelObj->keyToTable;
$belongsTo = $modelObj->belongsTo;
$hasOne = $modelObj->hasOne;
$hasMany = $modelObj->hasMany;
$hasAndBelongsToMany = $modelObj->hasAndBelongsToMany;
return compact('modelClass', 'primaryKey', 'displayField', 'singularVar', 'pluralVar', $fields = array_keys($modelObj->schema());
'singularHumanName', 'pluralHumanName', 'fields', 'foreignKeys', $associations = $this->__associations($modelObj);
'belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany');
return compact('fields', 'associations');
} }
/** /**
* Assembles and writes bakes the view file. * Assembles and writes bakes the view file.
@ -339,5 +326,24 @@ class ViewTask extends Shell {
$this->out(""); $this->out("");
exit(); exit();
} }
/**
* Returns associations for controllers models.
*
* @return array $associations
* @access private
*/
function __associations($model) {
$keys = array('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany');
foreach ($keys as $key => $type){
foreach ($model->{$type} as $assocKey => $assocData) {
$associations[$type][$assocKey]['primaryKey'] = $model->{$assocKey}->primaryKey;
$associations[$type][$assocKey]['displayField'] = $model->{$assocKey}->displayField;
$associations[$type][$assocKey]['foreignKey'] = $assocData['foreignKey'];
$associations[$type][$assocKey]['controller'] = Inflector::pluralize($assocData['className']);
}
}
return $associations;
}
} }
?> ?>

View file

@ -169,25 +169,10 @@ class Scaffold extends Object {
$this->scaffoldActions = $controller->scaffold; $this->scaffoldActions = $controller->scaffold;
$this->controller->pageTitle = __('Scaffold :: ', true) . Inflector::humanize($this->action) . ' :: ' . $this->scaffoldTitle; $this->controller->pageTitle = __('Scaffold :: ', true) . Inflector::humanize($this->action) . ' :: ' . $this->scaffoldTitle;
//template variables
$modelClass = $this->controller->modelClass;
$modelKey = $this->controller->modelKey;
$primaryKey = $this->ScaffoldModel->primaryKey;
$displayField = $this->ScaffoldModel->displayField;
$singularVar = Inflector::variable($modelClass);
$pluralVar = Inflector::variable($this->controller->name);
$singularHumanName = Inflector::humanize($modelClass);
$pluralHumanName = Inflector::humanize($this->controller->name);
$fields = array_keys($this->ScaffoldModel->schema()); $fields = array_keys($this->ScaffoldModel->schema());
$foreignKeys = $this->ScaffoldModel->keyToTable; $associations = $this->__associations();
$belongsTo = $this->ScaffoldModel->belongsTo;
$hasOne = $this->ScaffoldModel->hasOne;
$hasMany = $this->ScaffoldModel->hasMany;
$hasAndBelongsToMany = $this->ScaffoldModel->hasAndBelongsToMany;
$this->controller->set(compact('modelClass', 'primaryKey', 'displayField', 'singularVar', 'pluralVar', $this->controller->set(compact('fields', 'associations'));
'singularHumanName', 'pluralHumanName', 'fields', 'foreignKeys',
'belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany'));
$this->__scaffold($params); $this->__scaffold($params);
} }
/** /**
@ -508,5 +493,24 @@ class Scaffold extends Object {
} }
return LIBS . 'view' . DS . 'templates' . DS . 'scaffolds' . DS . $action . '.ctp'; return LIBS . 'view' . DS . 'templates' . DS . 'scaffolds' . DS . $action . '.ctp';
} }
/**
* Returns associations for controllers models.
*
* @return array $associations
* @access private
*/
function __associations() {
$keys = array('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany');
foreach ($keys as $key => $type){
foreach ($this->ScaffoldModel->{$type} as $assocKey => $assocData) {
$associations[$type][$assocKey]['primaryKey'] = $this->ScaffoldModel->{$assocKey}->primaryKey;
$associations[$type][$assocKey]['displayField'] = $this->ScaffoldModel->{$assocKey}->displayField;
$associations[$type][$assocKey]['foreignKey'] = $assocData['foreignKey'];
$associations[$type][$assocKey]['controller'] = Inflector::pluralize($assocData['className']);
}
}
return $associations;
}
} }
?> ?>

View file

@ -99,11 +99,6 @@ class Model extends Overloadable {
* @access protected * @access protected
*/ */
var $_schema = null; var $_schema = null;
/**
*
* @deprecated see $_schema
*/
var $_tableInfo = null;
/** /**
* List of validation rules. Append entries for validation as ('field_name' => '/^perl_compat_regexp$/') * List of validation rules. Append entries for validation as ('field_name' => '/^perl_compat_regexp$/')
* that have to match with preg_match(). Use these rules with Model::validate() * that have to match with preg_match(). Use these rules with Model::validate()
@ -145,20 +140,6 @@ class Model extends Overloadable {
* @access public * @access public
*/ */
var $tableToModel = array(); var $tableToModel = array();
/**
* List of Model names by used tables. Used for associations.
*
* @var array
* @access public
*/
var $modelToTable = array();
/**
* List of Foreign Key names to used tables. Used for associations.
*
* @var array
* @access public
*/
var $keyToTable = array();
/** /**
* Whether or not transactions for this model should be logged * Whether or not transactions for this model should be logged
* *
@ -678,7 +659,6 @@ class Model extends Overloadable {
} }
} }
$this->tableToModel[$this->{$assoc}->table] = $assoc; $this->tableToModel[$this->{$assoc}->table] = $assoc;
$this->modelToTable[$assoc] = $this->{$assoc}->table;
} }
/** /**
* Build array-based association from string. * Build array-based association from string.
@ -724,15 +704,6 @@ class Model extends Overloadable {
} }
$this->{$type}[$assocKey][$key] = $data; $this->{$type}[$assocKey][$key] = $data;
} }
if ($key == 'foreignKey' && !isset($this->keyToTable[$this->{$type}[$assocKey][$key]])) {
$this->keyToTable[$this->{$type}[$assocKey][$key]][0] = $this->{$class}->table;
$this->keyToTable[$this->{$type}[$assocKey][$key]][1] = $this->{$class}->alias;
if ($this->{$class}->alias != $class) {
$this->keyToTable[$this->{$type}[$assocKey][$key]][2] = $class;
}
}
} }
if (isset($this->{$type}[$assocKey]['with']) && !empty($this->{$type}[$assocKey]['with'])) { if (isset($this->{$type}[$assocKey]['with']) && !empty($this->{$type}[$assocKey]['with'])) {