2007-05-14 12:24:40 +00:00
|
|
|
<?php
|
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
/**
|
|
|
|
* The ModelTask handles creating and updating models files.
|
|
|
|
*
|
|
|
|
* Long description for file
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
|
2008-01-01 22:18:17 +00:00
|
|
|
* Copyright 2005-2008, Cake Software Foundation, Inc.
|
2007-05-14 12:24:40 +00:00
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
2008-01-01 22:18:17 +00:00
|
|
|
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc.
|
2007-05-14 12:24:40 +00:00
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.console.libs.tasks
|
|
|
|
* @since CakePHP(tm) v 1.2
|
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Task class for creating and updating model files.
|
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.console.libs.tasks
|
|
|
|
*/
|
|
|
|
class ModelTask extends Shell {
|
2008-03-09 01:05:27 +00:00
|
|
|
/**
|
|
|
|
* Name of plugin
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
2008-03-09 04:09:37 +00:00
|
|
|
*/
|
2008-03-09 01:05:27 +00:00
|
|
|
var $plugin = null;
|
2007-12-21 02:54:45 +00:00
|
|
|
/**
|
|
|
|
* path to MODELS directory
|
|
|
|
*
|
2007-12-22 19:24:56 +00:00
|
|
|
* @var string
|
2007-12-21 02:54:45 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $path = MODELS;
|
2008-03-15 05:45:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* tasks
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $tasks = array('DbConfig');
|
2007-06-03 23:56:33 +00:00
|
|
|
/**
|
|
|
|
* Execution method always used for tasks
|
|
|
|
*
|
2007-10-22 05:52:20 +00:00
|
|
|
* @access public
|
2007-06-03 23:56:33 +00:00
|
|
|
*/
|
2007-05-14 12:24:40 +00:00
|
|
|
function execute() {
|
2007-06-20 06:15:35 +00:00
|
|
|
if (empty($this->args)) {
|
2007-05-14 12:24:40 +00:00
|
|
|
$this->__interactive();
|
|
|
|
}
|
2007-09-10 11:18:52 +00:00
|
|
|
|
2007-07-25 04:38:28 +00:00
|
|
|
if (!empty($this->args[0])) {
|
2007-12-29 20:48:00 +00:00
|
|
|
$model = Inflector::camelize($this->args[0]);
|
2007-12-22 19:24:56 +00:00
|
|
|
if ($this->bake($model)) {
|
2007-07-05 08:50:56 +00:00
|
|
|
if ($this->_checkUnitTest()) {
|
2007-12-22 19:24:56 +00:00
|
|
|
$this->bakeTest($model);
|
2007-07-05 08:50:56 +00:00
|
|
|
}
|
2007-09-10 11:18:52 +00:00
|
|
|
}
|
2007-07-05 08:50:56 +00:00
|
|
|
}
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
2007-06-03 23:56:33 +00:00
|
|
|
/**
|
|
|
|
* Handles interactive baking
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*/
|
2007-05-14 12:24:40 +00:00
|
|
|
function __interactive() {
|
|
|
|
$this->hr();
|
2007-12-21 02:54:45 +00:00
|
|
|
$this->out(sprintf("Bake Model\nPath: %s", $this->path));
|
2007-05-14 12:24:40 +00:00
|
|
|
$this->hr();
|
|
|
|
$this->interactive = true;
|
|
|
|
|
|
|
|
$useTable = null;
|
|
|
|
$primaryKey = 'id';
|
|
|
|
$validate = array();
|
2007-11-22 07:21:46 +00:00
|
|
|
$associations = array('belongsTo'=> array(), 'hasOne'=> array(), 'hasMany' => array(), 'hasAndBelongsToMany'=> array());
|
|
|
|
|
|
|
|
$useDbConfig = 'default';
|
2008-03-15 05:45:43 +00:00
|
|
|
$configs = get_class_vars('DATABASE_CONFIG');
|
|
|
|
|
|
|
|
if (!is_array($configs)) {
|
|
|
|
return $this->DbConfig->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
$connections = array_keys($configs);
|
2007-12-22 19:24:56 +00:00
|
|
|
if (count($connections) > 1) {
|
2007-11-22 07:21:46 +00:00
|
|
|
$useDbConfig = $this->in(__('Use Database Config', true) .':', $connections, 'default');
|
|
|
|
}
|
2007-09-15 20:23:30 +00:00
|
|
|
|
|
|
|
$currentModelName = $this->getName($useDbConfig);
|
2007-05-14 12:24:40 +00:00
|
|
|
$db =& ConnectionManager::getDataSource($useDbConfig);
|
|
|
|
$useTable = Inflector::tableize($currentModelName);
|
|
|
|
$fullTableName = $db->fullTableName($useTable, false);
|
2007-12-22 19:24:56 +00:00
|
|
|
$tableIsGood = false;
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (array_search($useTable, $this->__tables) === false) {
|
2007-11-22 07:21:46 +00:00
|
|
|
$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');
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (low($tableIsGood) == 'n' || low($tableIsGood) == 'no') {
|
2007-11-22 07:21:46 +00:00
|
|
|
$useTable = $this->in(__('What is the name of the table (enter "null" to use NO table)?', true));
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
2007-12-22 19:24:56 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
while ($tableIsGood == false && low($useTable) != 'null') {
|
2007-05-14 12:24:40 +00:00
|
|
|
if (is_array($this->__tables) && !in_array($useTable, $this->__tables)) {
|
|
|
|
$fullTableName = $db->fullTableName($useTable, false);
|
|
|
|
$this->out($fullTableName . ' does not exist.');
|
2007-11-22 07:21:46 +00:00
|
|
|
$useTable = $this->in(__('What is the name of the table (enter "null" to use NO table)?', true));
|
2007-05-14 12:24:40 +00:00
|
|
|
$tableIsGood = false;
|
|
|
|
} else {
|
|
|
|
$tableIsGood = true;
|
|
|
|
}
|
|
|
|
}
|
2007-12-22 19:24:56 +00:00
|
|
|
|
2007-11-22 07:21:46 +00:00
|
|
|
$wannaDoValidation = $this->in(__('Would you like to supply validation criteria for the fields in your model?', true), array('y','n'), 'y');
|
2007-05-14 12:24:40 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (in_array($useTable, $this->__tables)) {
|
2007-11-17 17:37:23 +00:00
|
|
|
App::import('Model');
|
2007-12-22 19:24:56 +00:00
|
|
|
$tempModel = new Model(array('name' => $currentModelName, 'table' => $useTable, 'ds' => $useDbConfig));
|
2007-09-10 11:18:52 +00:00
|
|
|
|
2007-12-22 19:24:56 +00:00
|
|
|
$fields = $tempModel->schema();
|
|
|
|
if (!array_key_exists('id', $fields)) {
|
|
|
|
foreach ($fields as $name => $field) {
|
|
|
|
if (isset($field['key']) && $field['key'] == 'primary') {
|
|
|
|
break;
|
|
|
|
}
|
2007-09-10 11:18:52 +00:00
|
|
|
}
|
2007-11-22 07:21:46 +00:00
|
|
|
$primaryKey = $this->in(__('What is the primaryKey?', true), null, $name);
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_search($useTable, $this->__tables) !== false && (low($wannaDoValidation) == 'y' || low($wannaDoValidation) == 'yes')) {
|
2007-12-22 19:24:56 +00:00
|
|
|
$validate = $this->doValidation($tempModel);
|
|
|
|
}
|
|
|
|
|
|
|
|
$wannaDoAssoc = $this->in(__('Would you like to define model associations (hasMany, hasOne, belongsTo, etc.)?', true), array('y','n'), 'y');
|
|
|
|
if ((low($wannaDoAssoc) == 'y' || low($wannaDoAssoc) == 'yes')) {
|
|
|
|
$associations = $this->doAssociations($tempModel);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->out('');
|
|
|
|
$this->hr();
|
|
|
|
$this->out(__('The following Model will be created:', true));
|
|
|
|
$this->hr();
|
|
|
|
$this->out("Name: " . $currentModelName);
|
|
|
|
|
2008-01-20 03:42:01 +00:00
|
|
|
if ($useDbConfig !== 'default') {
|
|
|
|
$this->out("DB Config: " . $useDbConfig);
|
|
|
|
}
|
|
|
|
if ($fullTableName !== Inflector::tableize($currentModelName)) {
|
|
|
|
$this->out("DB Table: " . $fullTableName);
|
|
|
|
}
|
2007-12-22 19:24:56 +00:00
|
|
|
if ($primaryKey != 'id') {
|
|
|
|
$this->out("Primary Key: " . $primaryKey);
|
|
|
|
}
|
2008-01-20 03:42:01 +00:00
|
|
|
if (!empty($validate)) {
|
|
|
|
$this->out("Validation: " . print_r($validate, true));
|
|
|
|
}
|
2007-12-22 19:24:56 +00:00
|
|
|
if (!empty($associations)) {
|
|
|
|
$this->out("Associations:");
|
|
|
|
|
|
|
|
if (!empty($associations['belongsTo'])) {
|
|
|
|
for ($i = 0; $i < count($associations['belongsTo']); $i++) {
|
|
|
|
$this->out(" $currentModelName belongsTo {$associations['belongsTo'][$i]['alias']}");
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
2007-12-22 19:24:56 +00:00
|
|
|
}
|
2007-05-14 12:24:40 +00:00
|
|
|
|
2007-12-22 19:24:56 +00:00
|
|
|
if (!empty($associations['hasOne'])) {
|
|
|
|
for ($i = 0; $i < count($associations['hasOne']); $i++) {
|
|
|
|
$this->out(" $currentModelName hasOne {$associations['hasOne'][$i]['alias']}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($associations['hasMany'])) {
|
|
|
|
for ($i = 0; $i < count($associations['hasMany']); $i++) {
|
|
|
|
$this->out(" $currentModelName hasMany {$associations['hasMany'][$i]['alias']}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($associations['hasAndBelongsToMany'])) {
|
|
|
|
for ($i = 0; $i < count($associations['hasAndBelongsToMany']); $i++) {
|
|
|
|
$this->out(" $currentModelName hasAndBelongsToMany {$associations['hasAndBelongsToMany'][$i]['alias']}");
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-12-22 19:24:56 +00:00
|
|
|
$this->hr();
|
|
|
|
$looksGood = $this->in(__('Look okay?', true), array('y','n'), 'y');
|
2007-05-14 12:24:40 +00:00
|
|
|
|
2007-12-22 19:24:56 +00:00
|
|
|
if (low($looksGood) == 'y' || low($looksGood) == 'yes') {
|
|
|
|
if ($this->bake($currentModelName, $associations, $validate, $primaryKey, $useTable, $useDbConfig)) {
|
|
|
|
if ($this->_checkUnitTest()) {
|
2008-01-07 13:35:14 +00:00
|
|
|
$this->bakeTest($currentModelName, $useTable, $associations);
|
2007-12-22 19:24:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2008-03-15 05:45:43 +00:00
|
|
|
return false;
|
2007-12-22 19:24:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Handles associations
|
|
|
|
*
|
|
|
|
* @param object $model
|
|
|
|
* @param boolean $interactive
|
|
|
|
* @return array $validate
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function doValidation(&$model, $interactive = true) {
|
|
|
|
if (!is_object($model)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$fields = $model->schema();
|
2007-05-14 12:24:40 +00:00
|
|
|
|
2007-12-22 19:24:56 +00:00
|
|
|
if (empty($fields)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$validate = array();
|
|
|
|
$options = array('VALID_NOT_EMPTY', 'VALID_EMAIL', 'VALID_NUMER', 'VALID_YEAR');
|
|
|
|
|
|
|
|
if (class_exists('Validation')) {
|
|
|
|
$parent = get_class_methods(get_parent_class('Validation'));
|
|
|
|
$options = array_diff(get_class_methods('Validation'), $parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($fields as $fieldName => $field) {
|
|
|
|
$prompt = 'Field: ' . $fieldName . "\n";
|
|
|
|
$prompt .= 'Type: ' . $field['type'] . "\n";
|
|
|
|
$prompt .= '---------------------------------------------------------------'."\n";
|
|
|
|
$prompt .= 'Please select one of the following validation options:'."\n";
|
|
|
|
$prompt .= '---------------------------------------------------------------'."\n";
|
2008-03-16 04:59:40 +00:00
|
|
|
$choices = array();
|
2007-12-22 19:24:56 +00:00
|
|
|
$skip = 1;
|
2008-03-16 04:59:40 +00:00
|
|
|
sort($options);
|
2007-12-22 19:24:56 +00:00
|
|
|
foreach ($options as $key => $option) {
|
2008-03-16 04:59:40 +00:00
|
|
|
if ($option{0} != '_' && strtolower($option) != 'getinstance') {
|
|
|
|
$prompt .= "{$skip} - {$option}\n";
|
2008-03-16 05:41:09 +00:00
|
|
|
$choices[$skip] = strtolower($option);
|
2007-12-22 19:24:56 +00:00
|
|
|
$skip++;
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
|
|
|
}
|
2008-03-16 05:30:47 +00:00
|
|
|
$methods = array_flip($choices);
|
2008-03-16 06:07:16 +00:00
|
|
|
|
2007-12-22 19:24:56 +00:00
|
|
|
$prompt .= "{$skip} - Do not do any validation on this field.\n";
|
|
|
|
$prompt .= "... or enter in a valid regex validation string.\n";
|
|
|
|
|
|
|
|
$guess = $skip;
|
|
|
|
if ($field['null'] != 1 && $fieldName != $model->primaryKey && !in_array($fieldName, array('created', 'modified', 'updated'))) {
|
|
|
|
if ($fieldName == 'email') {
|
|
|
|
$guess = $methods['email'];
|
|
|
|
} elseif ($field['type'] == 'string') {
|
2008-03-16 05:41:09 +00:00
|
|
|
$guess = $methods['alphanumeric'];
|
2007-12-22 19:24:56 +00:00
|
|
|
} elseif ($field['type'] == 'integer') {
|
|
|
|
$guess = $methods['numeric'];
|
|
|
|
} elseif ($field['type'] == 'boolean') {
|
|
|
|
$guess = $methods['numeric'];
|
|
|
|
} elseif ($field['type'] == 'datetime') {
|
|
|
|
$guess = $methods['date'];
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
2007-12-22 19:24:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($interactive === true) {
|
|
|
|
$this->out('');
|
|
|
|
$choice = $this->in($prompt, null, $guess);
|
|
|
|
} else {
|
|
|
|
$choice = $guess;
|
|
|
|
}
|
2008-03-25 21:39:34 +00:00
|
|
|
if ($choice != $skip) {
|
2008-03-16 06:07:16 +00:00
|
|
|
if (is_numeric($choice) && isset($choices[$choice])) {
|
2008-03-16 04:59:40 +00:00
|
|
|
$validate[$fieldName] = $choices[$choice];
|
2007-12-22 19:24:56 +00:00
|
|
|
} else {
|
2008-03-09 04:32:03 +00:00
|
|
|
$validate[$fieldName] = $choice;
|
2007-12-22 19:24:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $validate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles associations
|
|
|
|
*
|
|
|
|
* @param object $model
|
|
|
|
* @param boolean $interactive
|
|
|
|
* @return array $assocaitons
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function doAssociations(&$model, $interactive = true) {
|
|
|
|
|
|
|
|
if (!is_object($model)) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-01-20 03:42:01 +00:00
|
|
|
$this->out(__('One moment while the associations are detected.', true));
|
2007-12-22 19:24:56 +00:00
|
|
|
|
|
|
|
$fields = $model->schema();
|
|
|
|
|
|
|
|
if (empty($fields)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$primaryKey = $model->primaryKey;
|
|
|
|
$foreignKey = $this->_modelKey($model->name);
|
|
|
|
|
|
|
|
$associations = $possibleKeys = array();
|
|
|
|
|
|
|
|
//Look for belongsTo
|
|
|
|
$i = 0;
|
|
|
|
foreach ($fields as $fieldName => $field) {
|
|
|
|
$offset = strpos($fieldName, '_id');
|
|
|
|
if ($fieldName != $model->primaryKey && $offset !== false) {
|
|
|
|
$tmpModelName = $this->_modelNameFromKey($fieldName);
|
|
|
|
$associations['belongsTo'][$i]['alias'] = $tmpModelName;
|
|
|
|
$associations['belongsTo'][$i]['className'] = $tmpModelName;
|
|
|
|
$associations['belongsTo'][$i]['foreignKey'] = $fieldName;
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//Look for hasOne and hasMany and hasAndBelongsToMany
|
|
|
|
$i = $j = 0;
|
|
|
|
|
|
|
|
foreach ($this->__tables as $otherTable) {
|
|
|
|
App::import('Model');
|
|
|
|
$tmpModelName = $this->_modelName($otherTable);
|
|
|
|
$tempOtherModel = & new Model(array('name' => $tmpModelName, 'table' => $otherTable, 'ds' => $model->useDbConfig));
|
|
|
|
$modelFieldsTemp = $tempOtherModel->schema();
|
|
|
|
|
|
|
|
$offset = strpos($otherTable, $model->table . '_');
|
|
|
|
$otherOffset = strpos($otherTable, '_' . $model->table);
|
|
|
|
|
|
|
|
foreach ($modelFieldsTemp as $fieldName => $field) {
|
|
|
|
if ($field['type'] == 'integer' || $field['type'] == 'string') {
|
|
|
|
$possibleKeys[$otherTable][] = $fieldName;
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
2007-12-22 19:24:56 +00:00
|
|
|
if ($fieldName != $model->primaryKey && $fieldName == $foreignKey && $offset === false && $otherOffset === false) {
|
|
|
|
$associations['hasOne'][$j]['alias'] = $tempOtherModel->name;
|
|
|
|
$associations['hasOne'][$j]['className'] = $tempOtherModel->name;
|
|
|
|
$associations['hasOne'][$j]['foreignKey'] = $fieldName;
|
|
|
|
|
|
|
|
$associations['hasMany'][$j]['alias'] = $tempOtherModel->name;
|
|
|
|
$associations['hasMany'][$j]['className'] = $tempOtherModel->name;
|
|
|
|
$associations['hasMany'][$j]['foreignKey'] = $fieldName;
|
|
|
|
$j++;
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
|
|
|
}
|
2007-11-22 07:21:46 +00:00
|
|
|
|
2007-12-22 19:24:56 +00:00
|
|
|
if ($offset !== false) {
|
|
|
|
$offset = strlen($model->table . '_');
|
|
|
|
$tmpModelName = $this->_modelName(substr($otherTable, $offset));
|
|
|
|
$associations['hasAndBelongsToMany'][$i]['alias'] = $tmpModelName;
|
|
|
|
$associations['hasAndBelongsToMany'][$i]['className'] = $tmpModelName;
|
|
|
|
$associations['hasAndBelongsToMany'][$i]['foreignKey'] = $foreignKey;
|
|
|
|
$associations['hasAndBelongsToMany'][$i]['associationForeignKey'] = $this->_modelKey($tmpModelName);
|
|
|
|
$associations['hasAndBelongsToMany'][$i]['joinTable'] = $otherTable;
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($otherOffset !== false) {
|
|
|
|
$tmpModelName = $this->_modelName(substr($otherTable, 0, $otherOffset));
|
|
|
|
$associations['hasAndBelongsToMany'][$i]['alias'] = $tmpModelName;
|
|
|
|
$associations['hasAndBelongsToMany'][$i]['className'] = $tmpModelName;
|
|
|
|
$associations['hasAndBelongsToMany'][$i]['foreignKey'] = $foreignKey;
|
|
|
|
$associations['hasAndBelongsToMany'][$i]['associationForeignKey'] = $this->_modelKey($tmpModelName);
|
|
|
|
$associations['hasAndBelongsToMany'][$i]['joinTable'] = $otherTable;
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($interactive !== true) {
|
|
|
|
unset($associations['hasOne']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($interactive === true) {
|
2007-05-14 12:24:40 +00:00
|
|
|
$this->hr();
|
2007-06-20 06:15:35 +00:00
|
|
|
if (empty($associations)) {
|
2007-11-22 07:21:46 +00:00
|
|
|
$this->out(__('None found.', true));
|
2007-05-14 12:24:40 +00:00
|
|
|
} else {
|
2007-11-22 07:21:46 +00:00
|
|
|
$this->out(__('Please confirm the following associations:', true));
|
2007-05-14 12:24:40 +00:00
|
|
|
$this->hr();
|
2007-11-22 07:21:46 +00:00
|
|
|
foreach ($associations as $type => $settings) {
|
|
|
|
if (!empty($associations[$type])) {
|
|
|
|
$count = count($associations[$type]);
|
|
|
|
$response = 'y';
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
2008-01-20 03:42:01 +00:00
|
|
|
$prompt = "{$model->name} {$type} {$associations[$type][$i]['alias']}";
|
|
|
|
$response = $this->in("{$prompt}?", array('y','n'), 'y');
|
|
|
|
|
2007-11-22 07:21:46 +00:00
|
|
|
if ('n' == low($response) || 'no' == low($response)) {
|
|
|
|
unset($associations[$type][$i]);
|
2008-01-20 03:42:01 +00:00
|
|
|
} else {
|
|
|
|
if ($model->name === $associations[$type][$i]['alias']) {
|
|
|
|
if ($type === 'belongsTo') {
|
|
|
|
$alias = 'Parent' . $associations[$type][$i]['alias'];
|
|
|
|
}
|
|
|
|
if($type === 'hasOne' || $type === 'hasMany') {
|
|
|
|
$alias = 'Child' . $associations[$type][$i]['alias'];
|
|
|
|
}
|
|
|
|
|
2008-03-22 21:49:49 +00:00
|
|
|
$alternateAlias = $this->in(sprintf(__('This is a self join. Use %s as the alias', true), $alias), array('y', 'n'), 'y');
|
2008-01-20 03:42:01 +00:00
|
|
|
|
|
|
|
if ('n' == low($alternateAlias) || 'no' == low($alternateAlias)) {
|
|
|
|
$associations[$type][$i]['alias'] = $this->in(__('Specify an alternate alias.', true));
|
|
|
|
} else {
|
|
|
|
$associations[$type][$i]['alias'] = $alias;
|
|
|
|
}
|
|
|
|
}
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
|
|
|
}
|
2007-11-22 07:21:46 +00:00
|
|
|
$associations[$type] = array_merge($associations[$type]);
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-11-22 07:21:46 +00:00
|
|
|
|
|
|
|
$wannaDoMoreAssoc = $this->in(__('Would you like to define some additional model associations?', true), array('y','n'), 'n');
|
2007-05-14 12:24:40 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
while ((low($wannaDoMoreAssoc) == 'y' || low($wannaDoMoreAssoc) == 'yes')) {
|
2007-05-29 06:30:08 +00:00
|
|
|
$assocs = array(1 => 'belongsTo', 2 => 'hasOne', 3 => 'hasMany', 4 => 'hasAndBelongsToMany');
|
2007-05-14 12:24:40 +00:00
|
|
|
$bad = true;
|
2007-06-20 06:15:35 +00:00
|
|
|
while ($bad) {
|
2007-11-22 07:21:46 +00:00
|
|
|
$this->out(__('What is the association type?', true));
|
|
|
|
$prompt = "1. belongsTo\n";
|
|
|
|
$prompt .= "2. hasOne\n";
|
|
|
|
$prompt .= "3. hasMany\n";
|
|
|
|
$prompt .= "4. hasAndBelongsToMany\n";
|
|
|
|
$assocType = intval($this->in($prompt, null, __("Enter a number", true)));
|
2007-05-14 12:24:40 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (intval($assocType) < 1 || intval($assocType) > 4) {
|
2007-11-22 07:21:46 +00:00
|
|
|
$this->out(__('The selection you entered was invalid. Please enter a number between 1 and 4.', true));
|
2007-05-14 12:24:40 +00:00
|
|
|
} else {
|
|
|
|
$bad = false;
|
|
|
|
}
|
|
|
|
}
|
2007-11-22 07:21:46 +00:00
|
|
|
$this->out(__('For the following options be very careful to match your setup exactly. Any spelling mistakes will cause errors.', true));
|
2007-05-14 12:24:40 +00:00
|
|
|
$this->hr();
|
2007-11-22 07:21:46 +00:00
|
|
|
$alias = $this->in(__('What is the alias for this association?', true));
|
|
|
|
$className = $this->in(sprintf(__('What className will %s use?', true), $alias), null, $alias );
|
2007-05-14 12:24:40 +00:00
|
|
|
$suggestedForeignKey = null;
|
2007-06-20 06:15:35 +00:00
|
|
|
if ($assocType == '1') {
|
2007-12-22 19:24:56 +00:00
|
|
|
$showKeys = $possibleKeys[$model->table];
|
2007-11-22 07:21:46 +00:00
|
|
|
$suggestedForeignKey = $this->_modelKey($alias);
|
2007-05-14 12:24:40 +00:00
|
|
|
} else {
|
|
|
|
$otherTable = Inflector::tableize($className);
|
2007-06-20 06:15:35 +00:00
|
|
|
if (in_array($otherTable, $this->__tables)) {
|
|
|
|
if ($assocType < '4') {
|
2007-05-14 12:24:40 +00:00
|
|
|
$showKeys = $possibleKeys[$otherTable];
|
|
|
|
} else {
|
|
|
|
$showKeys = null;
|
|
|
|
}
|
|
|
|
} else {
|
2007-11-22 07:21:46 +00:00
|
|
|
$otherTable = $this->in(__('What is the table for this model?', true));
|
2007-05-14 12:24:40 +00:00
|
|
|
$showKeys = $possibleKeys[$otherTable];
|
|
|
|
}
|
2007-12-22 19:24:56 +00:00
|
|
|
$suggestedForeignKey = $this->_modelKey($model->name);
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
2007-06-20 06:15:35 +00:00
|
|
|
if (!empty($showKeys)) {
|
2007-11-22 07:21:46 +00:00
|
|
|
$this->out(__('A helpful List of possible keys', true));
|
2007-05-14 12:24:40 +00:00
|
|
|
for ($i = 0; $i < count($showKeys); $i++) {
|
|
|
|
$this->out($i + 1 . ". " . $showKeys[$i]);
|
|
|
|
}
|
2007-11-22 07:21:46 +00:00
|
|
|
$foreignKey = $this->in(__('What is the foreignKey?', true), null, __("Enter a number", true));
|
2007-05-14 12:24:40 +00:00
|
|
|
if (intval($foreignKey) > 0 && intval($foreignKey) <= $i ) {
|
|
|
|
$foreignKey = $showKeys[intval($foreignKey) - 1];
|
|
|
|
}
|
|
|
|
}
|
2007-06-20 06:15:35 +00:00
|
|
|
if (!isset($foreignKey)) {
|
2007-11-22 07:21:46 +00:00
|
|
|
$foreignKey = $this->in(__('What is the foreignKey? Specify your own.', true), null, $suggestedForeignKey);
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
2007-06-20 06:15:35 +00:00
|
|
|
if ($assocType == '4') {
|
2007-12-22 19:24:56 +00:00
|
|
|
$associationForeignKey = $this->in(__('What is the associationForeignKey?', true), null, $this->_modelKey($model->name));
|
2007-11-22 07:21:46 +00:00
|
|
|
$joinTable = $this->in(__('What is the joinTable?', true));
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
2008-01-20 02:54:32 +00:00
|
|
|
$associations[$assocs[$assocType]] = array_values((array)$associations[$assocs[$assocType]]);
|
2007-05-14 12:24:40 +00:00
|
|
|
$count = count($associations[$assocs[$assocType]]);
|
|
|
|
$i = ($count > 0) ? $count : 0;
|
2007-11-22 07:21:46 +00:00
|
|
|
$associations[$assocs[$assocType]][$i]['alias'] = $alias;
|
2007-05-14 12:24:40 +00:00
|
|
|
$associations[$assocs[$assocType]][$i]['className'] = $className;
|
|
|
|
$associations[$assocs[$assocType]][$i]['foreignKey'] = $foreignKey;
|
2007-06-20 06:15:35 +00:00
|
|
|
if ($assocType == '4') {
|
2007-05-14 12:24:40 +00:00
|
|
|
$associations[$assocs[$assocType]][$i]['associationForeignKey'] = $associationForeignKey;
|
|
|
|
$associations[$assocs[$assocType]][$i]['joinTable'] = $joinTable;
|
|
|
|
}
|
2007-11-22 07:21:46 +00:00
|
|
|
$wannaDoMoreAssoc = $this->in(__('Define another association?', true), array('y','n'), 'y');
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
|
|
|
}
|
2007-12-22 19:24:56 +00:00
|
|
|
return $associations;
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Assembles and writes a Model file.
|
|
|
|
*
|
2007-12-22 19:24:56 +00:00
|
|
|
* @param mixed $name Model name or object
|
|
|
|
* @param mixed $associations if array and $name is not an object assume Model associations array otherwise boolean interactive
|
2007-10-22 05:52:20 +00:00
|
|
|
* @param array $validate Validation rules
|
2007-12-22 19:24:56 +00:00
|
|
|
* @param string $primaryKey Primary key to use
|
|
|
|
* @param string $useTable Table to use
|
|
|
|
* @param string $useDbConfig Database configuration setting to use
|
2007-10-22 05:52:20 +00:00
|
|
|
* @access private
|
2007-05-14 12:24:40 +00:00
|
|
|
*/
|
2007-12-22 19:24:56 +00:00
|
|
|
function bake($name, $associations = array(), $validate = array(), $primaryKey = 'id', $useTable = null, $useDbConfig = 'default') {
|
|
|
|
|
|
|
|
if (is_object($name)) {
|
|
|
|
if (!is_array($associations)) {
|
|
|
|
$associations = $this->doAssociations($name, $associations);
|
|
|
|
$validate = $this->doValidation($name, $associations);
|
|
|
|
}
|
|
|
|
$primaryKey = $name->primaryKey;
|
|
|
|
$useTable = $name->table;
|
|
|
|
$useDbConfig = $name->useDbConfig;
|
|
|
|
$name = $name->name;
|
|
|
|
}
|
|
|
|
|
2007-05-14 12:24:40 +00:00
|
|
|
$out = "<?php\n";
|
2008-03-09 01:05:27 +00:00
|
|
|
$out .= "class {$name} extends {$this->plugin}AppModel {\n\n";
|
2007-05-14 12:24:40 +00:00
|
|
|
$out .= "\tvar \$name = '{$name}';\n";
|
|
|
|
|
2007-11-22 07:21:46 +00:00
|
|
|
if ($useDbConfig !== 'default') {
|
2007-05-14 12:24:40 +00:00
|
|
|
$out .= "\tvar \$useDbConfig = '$useDbConfig';\n";
|
|
|
|
}
|
|
|
|
|
2008-01-06 19:39:25 +00:00
|
|
|
if (($useTable && $useTable !== Inflector::tableize($name)) || $useTable === false) {
|
|
|
|
$table = "'$useTable'";
|
|
|
|
if (!$useTable) {
|
|
|
|
$table = 'false';
|
|
|
|
}
|
|
|
|
$out .= "\tvar \$useTable = $table;\n";
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
|
|
|
|
2007-11-22 07:21:46 +00:00
|
|
|
if ($primaryKey !== 'id') {
|
2007-05-14 12:24:40 +00:00
|
|
|
$out .= "\tvar \$primaryKey = '$primaryKey';\n";
|
|
|
|
}
|
|
|
|
|
2007-12-22 19:24:56 +00:00
|
|
|
$validateCount = count($validate);
|
2008-01-22 06:49:10 +00:00
|
|
|
if (is_array($validate) && $validateCount > 0) {
|
2007-05-14 12:24:40 +00:00
|
|
|
$out .= "\tvar \$validate = array(\n";
|
|
|
|
$keys = array_keys($validate);
|
2007-12-22 19:24:56 +00:00
|
|
|
for ($i = 0; $i < $validateCount; $i++) {
|
|
|
|
$out .= "\t\t'" . $keys[$i] . "' => array('" . $validate[$keys[$i]] . "')";
|
|
|
|
if ($i + 1 < $validateCount) {
|
|
|
|
$out .= ",";
|
|
|
|
}
|
|
|
|
$out .= "\n";
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
|
|
|
$out .= "\t);\n";
|
|
|
|
}
|
|
|
|
$out .= "\n";
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (!empty($associations)) {
|
2007-12-22 19:24:56 +00:00
|
|
|
if (!empty($associations['belongsTo']) || !empty($associations['$hasOne']) || !empty($associations['hasMany']) || !empty($associations['hasAndBelongsToMany'])) {
|
2007-09-16 19:56:55 +00:00
|
|
|
$out.= "\t//The Associations below have been created with all possible keys, those that are not needed can be removed\n";
|
|
|
|
}
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (!empty($associations['belongsTo'])) {
|
2007-05-14 12:24:40 +00:00
|
|
|
$out .= "\tvar \$belongsTo = array(\n";
|
2007-12-22 19:24:56 +00:00
|
|
|
$belongsToCount = count($associations['belongsTo']);
|
2007-05-14 12:24:40 +00:00
|
|
|
|
2007-12-22 19:24:56 +00:00
|
|
|
for ($i = 0; $i < $belongsToCount; $i++) {
|
2007-05-14 12:24:40 +00:00
|
|
|
$out .= "\t\t\t'{$associations['belongsTo'][$i]['alias']}' => ";
|
|
|
|
$out .= "array('className' => '{$associations['belongsTo'][$i]['className']}',\n";
|
|
|
|
$out .= "\t\t\t\t\t\t\t\t'foreignKey' => '{$associations['belongsTo'][$i]['foreignKey']}',\n";
|
|
|
|
$out .= "\t\t\t\t\t\t\t\t'conditions' => '',\n";
|
|
|
|
$out .= "\t\t\t\t\t\t\t\t'fields' => '',\n";
|
2007-12-22 19:24:56 +00:00
|
|
|
$out .= "\t\t\t\t\t\t\t\t'order' => ''\n";
|
|
|
|
$out .= "\t\t\t)";
|
|
|
|
if ($i + 1 < $belongsToCount) {
|
|
|
|
$out .= ",";
|
|
|
|
}
|
|
|
|
$out .= "\n";
|
|
|
|
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
|
|
|
$out .= "\t);\n\n";
|
|
|
|
}
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (!empty($associations['hasOne'])) {
|
2007-05-14 12:24:40 +00:00
|
|
|
$out .= "\tvar \$hasOne = array(\n";
|
2007-12-22 19:24:56 +00:00
|
|
|
$hasOneCount = count($associations['hasOne']);
|
2007-05-14 12:24:40 +00:00
|
|
|
|
2007-12-22 19:24:56 +00:00
|
|
|
for ($i = 0; $i < $hasOneCount; $i++) {
|
2007-05-14 12:24:40 +00:00
|
|
|
$out .= "\t\t\t'{$associations['hasOne'][$i]['alias']}' => ";
|
|
|
|
$out .= "array('className' => '{$associations['hasOne'][$i]['className']}',\n";
|
|
|
|
$out .= "\t\t\t\t\t\t\t\t'foreignKey' => '{$associations['hasOne'][$i]['foreignKey']}',\n";
|
2007-12-22 19:24:56 +00:00
|
|
|
$out .= "\t\t\t\t\t\t\t\t'dependent' => false,\n";
|
2007-05-14 12:24:40 +00:00
|
|
|
$out .= "\t\t\t\t\t\t\t\t'conditions' => '',\n";
|
|
|
|
$out .= "\t\t\t\t\t\t\t\t'fields' => '',\n";
|
2007-12-22 19:24:56 +00:00
|
|
|
$out .= "\t\t\t\t\t\t\t\t'order' => ''\n";
|
|
|
|
$out .= "\t\t\t)";
|
|
|
|
if ($i + 1 < $hasOneCount) {
|
|
|
|
$out .= ",";
|
|
|
|
}
|
|
|
|
$out .= "\n";
|
|
|
|
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
|
|
|
$out .= "\t);\n\n";
|
|
|
|
}
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (!empty($associations['hasMany'])) {
|
2007-05-14 12:24:40 +00:00
|
|
|
$out .= "\tvar \$hasMany = array(\n";
|
2007-12-22 19:24:56 +00:00
|
|
|
$hasManyCount = count($associations['hasMany']);
|
2007-05-14 12:24:40 +00:00
|
|
|
|
2007-12-22 19:24:56 +00:00
|
|
|
for ($i = 0; $i < $hasManyCount; $i++) {
|
2007-05-14 12:24:40 +00:00
|
|
|
$out .= "\t\t\t'{$associations['hasMany'][$i]['alias']}' => ";
|
|
|
|
$out .= "array('className' => '{$associations['hasMany'][$i]['className']}',\n";
|
|
|
|
$out .= "\t\t\t\t\t\t\t\t'foreignKey' => '{$associations['hasMany'][$i]['foreignKey']}',\n";
|
2007-12-22 19:24:56 +00:00
|
|
|
$out .= "\t\t\t\t\t\t\t\t'dependent' => false,\n";
|
2007-05-14 12:24:40 +00:00
|
|
|
$out .= "\t\t\t\t\t\t\t\t'conditions' => '',\n";
|
|
|
|
$out .= "\t\t\t\t\t\t\t\t'fields' => '',\n";
|
|
|
|
$out .= "\t\t\t\t\t\t\t\t'order' => '',\n";
|
|
|
|
$out .= "\t\t\t\t\t\t\t\t'limit' => '',\n";
|
|
|
|
$out .= "\t\t\t\t\t\t\t\t'offset' => '',\n";
|
|
|
|
$out .= "\t\t\t\t\t\t\t\t'exclusive' => '',\n";
|
|
|
|
$out .= "\t\t\t\t\t\t\t\t'finderQuery' => '',\n";
|
2007-12-22 19:24:56 +00:00
|
|
|
$out .= "\t\t\t\t\t\t\t\t'counterQuery' => ''\n";
|
|
|
|
$out .= "\t\t\t)";
|
|
|
|
if ($i + 1 < $hasManyCount) {
|
|
|
|
$out .= ",";
|
|
|
|
}
|
|
|
|
$out .= "\n";
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
|
|
|
$out .= "\t);\n\n";
|
|
|
|
}
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (!empty($associations['hasAndBelongsToMany'])) {
|
2007-05-14 12:24:40 +00:00
|
|
|
$out .= "\tvar \$hasAndBelongsToMany = array(\n";
|
2007-12-22 19:24:56 +00:00
|
|
|
$hasAndBelongsToManyCount = count($associations['hasAndBelongsToMany']);
|
2007-05-14 12:24:40 +00:00
|
|
|
|
2007-12-22 19:24:56 +00:00
|
|
|
for ($i = 0; $i < $hasAndBelongsToManyCount; $i++) {
|
2007-05-14 12:24:40 +00:00
|
|
|
$out .= "\t\t\t'{$associations['hasAndBelongsToMany'][$i]['alias']}' => ";
|
|
|
|
$out .= "array('className' => '{$associations['hasAndBelongsToMany'][$i]['className']}',\n";
|
|
|
|
$out .= "\t\t\t\t\t\t'joinTable' => '{$associations['hasAndBelongsToMany'][$i]['joinTable']}',\n";
|
|
|
|
$out .= "\t\t\t\t\t\t'foreignKey' => '{$associations['hasAndBelongsToMany'][$i]['foreignKey']}',\n";
|
|
|
|
$out .= "\t\t\t\t\t\t'associationForeignKey' => '{$associations['hasAndBelongsToMany'][$i]['associationForeignKey']}',\n";
|
2007-12-22 19:24:56 +00:00
|
|
|
$out .= "\t\t\t\t\t\t'unique' => true,\n";
|
2007-05-14 12:24:40 +00:00
|
|
|
$out .= "\t\t\t\t\t\t'conditions' => '',\n";
|
|
|
|
$out .= "\t\t\t\t\t\t'fields' => '',\n";
|
|
|
|
$out .= "\t\t\t\t\t\t'order' => '',\n";
|
|
|
|
$out .= "\t\t\t\t\t\t'limit' => '',\n";
|
|
|
|
$out .= "\t\t\t\t\t\t'offset' => '',\n";
|
|
|
|
$out .= "\t\t\t\t\t\t'finderQuery' => '',\n";
|
|
|
|
$out .= "\t\t\t\t\t\t'deleteQuery' => '',\n";
|
2007-12-22 19:24:56 +00:00
|
|
|
$out .= "\t\t\t\t\t\t'insertQuery' => ''\n";
|
|
|
|
$out .= "\t\t\t)";
|
|
|
|
if ($i + 1 < $hasAndBelongsToManyCount) {
|
|
|
|
$out .= ",";
|
|
|
|
}
|
|
|
|
$out .= "\n";
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
|
|
|
$out .= "\t);\n\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$out .= "}\n";
|
|
|
|
$out .= "?>";
|
2007-12-21 02:54:45 +00:00
|
|
|
$filename = $this->path . Inflector::underscore($name) . '.php';
|
2008-01-05 19:31:29 +00:00
|
|
|
$this->out("\nBaking model class for $name...");
|
2007-05-14 12:24:40 +00:00
|
|
|
return $this->createFile($filename, $out);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-10-22 05:52:20 +00:00
|
|
|
* Assembles and writes a unit test file
|
2007-05-14 12:24:40 +00:00
|
|
|
*
|
2007-10-22 05:52:20 +00:00
|
|
|
* @param string $className Model class name
|
|
|
|
* @access private
|
2007-05-14 12:24:40 +00:00
|
|
|
*/
|
2008-01-07 13:35:14 +00:00
|
|
|
function bakeTest($className, $useTable = null, $associations = array()) {
|
2008-01-06 19:39:25 +00:00
|
|
|
$results = $this->fixture($className, $useTable);
|
2008-01-05 19:31:29 +00:00
|
|
|
|
|
|
|
if ($results) {
|
2008-03-09 04:09:37 +00:00
|
|
|
$fixtureInc = 'app';
|
|
|
|
if ($this->plugin) {
|
|
|
|
$fixtureInc = 'plugin.'.Inflector::underscore($this->plugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
$fixture[] = "'{$fixtureInc}." . Inflector::underscore($className) ."'";
|
2008-01-07 13:35:14 +00:00
|
|
|
|
|
|
|
if (!empty($associations)) {
|
|
|
|
$assoc[] = Set::extract($associations, 'belongsTo.{n}.className');
|
|
|
|
$assoc[] = Set::extract($associations, 'hasOne.{n}.className');
|
|
|
|
$assoc[] = Set::extract($associations, 'hasMany.{n}.className');
|
|
|
|
foreach ($assoc as $key => $value) {
|
|
|
|
if (is_array($value)) {
|
|
|
|
foreach ($value as $class) {
|
2008-03-09 04:09:37 +00:00
|
|
|
$fixture[] = "'{$fixtureInc}" . Inflector::underscore($class) ."'";
|
2008-01-07 13:35:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$fixture = join(", ", $fixture);
|
2008-03-09 04:09:37 +00:00
|
|
|
|
2008-03-09 01:05:27 +00:00
|
|
|
$import = $className;
|
|
|
|
if (isset($this->plugin)) {
|
|
|
|
$import = $this->plugin . '.' . $className;
|
|
|
|
}
|
2008-03-09 04:09:37 +00:00
|
|
|
|
2008-03-09 01:05:27 +00:00
|
|
|
$out = "App::import('Model', '$import');\n\n";
|
2008-01-05 19:31:29 +00:00
|
|
|
$out .= "class Test{$className} extends {$className} {\n";
|
2008-04-01 18:00:35 +00:00
|
|
|
$out .= "\tvar \$cacheSources = false;\n";
|
|
|
|
$out .= "\tvar \$useDbConfig = 'test_suite';\n}\n\n";
|
2008-01-05 19:31:29 +00:00
|
|
|
$out .= "class {$className}TestCase extends CakeTestCase {\n";
|
2008-01-07 13:35:14 +00:00
|
|
|
$out .= "\tvar \${$className} = null;\n";
|
|
|
|
$out .= "\tvar \$fixtures = array($fixture);\n\n";
|
2008-01-05 19:31:29 +00:00
|
|
|
$out .= "\tfunction start() {\n\t\tparent::start();\n\t\t\$this->{$className} = new Test{$className}();\n\t}\n\n";
|
|
|
|
$out .= "\tfunction test{$className}Instance() {\n";
|
|
|
|
$out .= "\t\t\$this->assertTrue(is_a(\$this->{$className}, '{$className}'));\n\t}\n\n";
|
|
|
|
$out .= "\tfunction test{$className}Find() {\n";
|
2008-01-07 13:35:14 +00:00
|
|
|
$out .= "\t\t\$results = \$this->{$className}->recursive = -1;\n";
|
2008-01-05 19:31:29 +00:00
|
|
|
$out .= "\t\t\$results = \$this->{$className}->find('first');\n\t\t\$this->assertTrue(!empty(\$results));\n\n";
|
|
|
|
$out .= "\t\t\$expected = array('$className' => array(\n$results\n\t\t\t));\n";
|
|
|
|
$out .= "\t\t\$this->assertEqual(\$results, \$expected);\n\t}\n}\n";
|
|
|
|
|
|
|
|
$path = MODEL_TESTS;
|
2008-03-09 01:05:27 +00:00
|
|
|
if (isset($this->plugin)) {
|
|
|
|
$pluginPath = 'plugins' . DS . Inflector::underscore($this->plugin) . DS;
|
2008-03-09 04:09:37 +00:00
|
|
|
$path = APP . $pluginPath . 'tests' . DS . 'cases' . DS . 'models' . DS;
|
2008-03-09 01:05:27 +00:00
|
|
|
}
|
2008-03-09 04:09:37 +00:00
|
|
|
|
2008-01-05 19:31:29 +00:00
|
|
|
$filename = Inflector::underscore($className).'.test.php';
|
|
|
|
$this->out("\nBaking unit test for $className...");
|
|
|
|
|
2008-01-06 01:50:23 +00:00
|
|
|
$header = '$Id';
|
2008-01-05 19:31:29 +00:00
|
|
|
$content = "<?php \n/* SVN FILE: $header$ */\n/* ". $className ." Test cases generated on: " . date('Y-m-d H:m:s') . " : ". time() . "*/\n{$out}?>";
|
|
|
|
return $this->createFile($path . $filename, $content);
|
|
|
|
}
|
|
|
|
return false;
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* outputs the a list of possible models or controllers from database
|
|
|
|
*
|
2007-10-22 05:52:20 +00:00
|
|
|
* @param string $useDbConfig Database configuration name
|
|
|
|
* @access public
|
2007-05-14 12:24:40 +00:00
|
|
|
*/
|
2007-12-22 19:24:56 +00:00
|
|
|
function listAll($useDbConfig = 'default', $interactive = true) {
|
2007-05-14 12:24:40 +00:00
|
|
|
$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();
|
|
|
|
}
|
2008-03-15 05:45:43 +00:00
|
|
|
if (empty($tables)) {
|
|
|
|
$this->err(__('Your database does not have any tables.', true));
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
2007-05-14 12:24:40 +00:00
|
|
|
$this->__tables = $tables;
|
2007-12-22 19:24:56 +00:00
|
|
|
|
|
|
|
if ($interactive === true) {
|
|
|
|
$this->out(__('Possible Models based on your current database:', true));
|
|
|
|
$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]);
|
|
|
|
}
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
|
|
|
}
|
2007-05-26 09:00:54 +00:00
|
|
|
/**
|
|
|
|
* Forces the user to specify the model he wants to bake, and returns the selected model name.
|
|
|
|
*
|
2007-10-22 05:52:20 +00:00
|
|
|
* @return string the model name
|
|
|
|
* @access public
|
2007-05-26 09:00:54 +00:00
|
|
|
*/
|
2007-06-03 23:56:33 +00:00
|
|
|
function getName($useDbConfig) {
|
|
|
|
$this->listAll($useDbConfig);
|
2007-05-26 09:00:54 +00:00
|
|
|
|
|
|
|
$enteredModel = '';
|
|
|
|
|
|
|
|
while ($enteredModel == '') {
|
2008-02-06 00:44:34 +00:00
|
|
|
$enteredModel = $this->in(__("Enter a number from the list above, type in the name of another model, or 'q' to exit", true), null, 'q');
|
2008-03-09 04:09:37 +00:00
|
|
|
|
2008-02-06 00:44:34 +00:00
|
|
|
if ($enteredModel === 'q') {
|
|
|
|
$this->out(__("Exit", true));
|
|
|
|
exit();
|
|
|
|
}
|
2008-03-09 04:09:37 +00:00
|
|
|
|
2007-05-26 09:00:54 +00:00
|
|
|
if ($enteredModel == '' || intval($enteredModel) > count($this->_modelNames)) {
|
2007-11-22 07:21:46 +00:00
|
|
|
$this->err(__("The model name you supplied was empty, or the number you selected was not an option. Please try again.", true));
|
2007-05-26 09:00:54 +00:00
|
|
|
$enteredModel = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (intval($enteredModel) > 0 && intval($enteredModel) <= count($this->_modelNames)) {
|
|
|
|
$currentModelName = $this->_modelNames[intval($enteredModel) - 1];
|
|
|
|
} else {
|
|
|
|
$currentModelName = $enteredModel;
|
|
|
|
}
|
2007-05-29 06:30:08 +00:00
|
|
|
|
2007-05-26 09:00:54 +00:00
|
|
|
return $currentModelName;
|
|
|
|
}
|
2007-06-08 08:44:34 +00:00
|
|
|
/**
|
|
|
|
* Displays help contents
|
|
|
|
*
|
2007-10-22 05:52:20 +00:00
|
|
|
* @access public
|
2007-06-08 08:44:34 +00:00
|
|
|
*/
|
|
|
|
function help() {
|
|
|
|
$this->hr();
|
2007-11-22 07:21:46 +00:00
|
|
|
$this->out("Usage: cake bake model <arg1>");
|
2007-06-08 08:44:34 +00:00
|
|
|
$this->hr();
|
2007-11-22 07:21:46 +00:00
|
|
|
$this->out('Commands:');
|
|
|
|
$this->out("\n\tmodel\n\t\tbakes model in interactive mode.");
|
|
|
|
$this->out("\n\tmodel <name>\n\t\tbakes model file with no associations or validation");
|
2007-06-08 08:44:34 +00:00
|
|
|
$this->out("");
|
|
|
|
exit();
|
|
|
|
}
|
2008-01-05 19:31:29 +00:00
|
|
|
/**
|
|
|
|
* Builds the tests fixtures for the model and create the file
|
|
|
|
*
|
|
|
|
* @param string $model the name of the model
|
2008-01-06 19:39:25 +00:00
|
|
|
* @param string $useTable table name
|
2008-01-05 19:31:29 +00:00
|
|
|
* @return array $records, used in ModelTask::bakeTest() to create $expected
|
|
|
|
* @todo move this to a task
|
|
|
|
*/
|
2008-01-06 19:39:25 +00:00
|
|
|
function fixture($model, $useTable = null) {
|
2008-01-05 19:31:29 +00:00
|
|
|
if (!class_exists('CakeSchema')) {
|
|
|
|
App::import('Model', 'Schema');
|
|
|
|
}
|
2008-01-07 13:35:14 +00:00
|
|
|
$out = "\nclass {$model}Fixture extends CakeTestFixture {\n";
|
|
|
|
$out .= "\tvar \$name = '$model';\n";
|
|
|
|
|
2008-01-06 19:39:25 +00:00
|
|
|
if (!$useTable) {
|
|
|
|
$useTable = Inflector::tableize($model);
|
2008-01-07 13:35:14 +00:00
|
|
|
} else {
|
|
|
|
$out .= "\tvar \$table = '$useTable';\n";
|
2008-01-06 19:39:25 +00:00
|
|
|
}
|
2008-01-05 19:31:29 +00:00
|
|
|
$schema = new CakeSchema();
|
|
|
|
$data = $schema->read(array('models' => false));
|
2008-01-07 13:35:14 +00:00
|
|
|
|
2008-01-19 22:04:54 +00:00
|
|
|
if (!isset($data['tables'][$useTable])) {
|
2008-01-07 13:35:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
2008-01-19 22:04:54 +00:00
|
|
|
$tables[$model] = $data['tables'][$useTable];
|
2008-01-06 19:39:25 +00:00
|
|
|
|
2008-01-05 19:31:29 +00:00
|
|
|
foreach ($tables as $table => $fields) {
|
|
|
|
if (!is_numeric($table) && $table !== 'missing') {
|
|
|
|
$out .= "\tvar \$fields = array(\n";
|
|
|
|
$records = array();
|
|
|
|
if (is_array($fields)) {
|
|
|
|
$cols = array();
|
|
|
|
foreach ($fields as $field => $value) {
|
|
|
|
if ($field != 'indexes') {
|
|
|
|
if (is_string($value)) {
|
|
|
|
$type = $value;
|
|
|
|
$value = array('type'=> $type);
|
|
|
|
}
|
|
|
|
$col = "\t\t\t'{$field}' => array('type'=>'" . $value['type'] . "', ";
|
|
|
|
|
|
|
|
switch ($value['type']) {
|
|
|
|
case 'integer':
|
|
|
|
$insert = 1;
|
|
|
|
break;
|
|
|
|
case 'string';
|
2008-01-16 19:38:24 +00:00
|
|
|
$insert = "Lorem ipsum dolor sit amet";
|
|
|
|
if (!empty($value['length'])) {
|
|
|
|
$insert = substr($insert, 0, (int)$value['length'] - 2);
|
|
|
|
}
|
|
|
|
$insert = "'$insert'";
|
2008-01-05 19:31:29 +00:00
|
|
|
break;
|
|
|
|
case 'datetime':
|
|
|
|
$ts = date('Y-m-d H:i:s');
|
|
|
|
$insert = "'$ts'";
|
|
|
|
break;
|
2008-03-09 04:09:37 +00:00
|
|
|
case 'date':
|
|
|
|
$ts = date('Y-m-d');
|
|
|
|
$insert = "'$ts'";
|
|
|
|
break;
|
|
|
|
case 'time':
|
|
|
|
$ts = date('H:i:s');
|
|
|
|
$insert = "'$ts'";
|
|
|
|
break;
|
2008-01-05 19:31:29 +00:00
|
|
|
case 'boolean':
|
|
|
|
$insert = 1;
|
|
|
|
break;
|
|
|
|
case 'text':
|
|
|
|
$insert =
|
|
|
|
'\'Lorem ipsum dolor sit amet, aliquet feugiat. Convallis morbi fringilla gravida,
|
|
|
|
phasellus feugiat dapibus velit nunc, pulvinar eget sollicitudin venenatis cum nullam,
|
|
|
|
vivamus ut a sed, mollitia lectus. Nulla vestibulum massa neque ut et, id hendrerit sit,
|
|
|
|
feugiat in taciti enim proin nibh, tempor dignissim, rhoncus duis vestibulum nunc mattis convallis.
|
|
|
|
Orci aliquet, in lorem et velit maecenas luctus, wisi nulla at, mauris nam ut a, lorem et et elit eu.
|
|
|
|
Sed dui facilisi, adipiscing mollis lacus congue integer, faucibus consectetuer eros amet sit sit,
|
|
|
|
magna dolor posuere. Placeat et, ac occaecat rutrum ante ut fusce. Sit velit sit porttitor non enim purus,
|
|
|
|
id semper consectetuer justo enim, nulla etiam quis justo condimentum vel, malesuada ligula arcu. Nisl neque,
|
|
|
|
ligula cras suscipit nunc eget, et tellus in varius urna odio est. Fuga urna dis metus euismod laoreet orci,
|
|
|
|
litora luctus suspendisse sed id luctus ut. Pede volutpat quam vitae, ut ornare wisi. Velit dis tincidunt,
|
|
|
|
pede vel eleifend nec curabitur dui pellentesque, volutpat taciti aliquet vivamus viverra, eget tellus ut
|
|
|
|
feugiat lacinia mauris sed, lacinia et felis.\'';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$records[] = "\t\t\t'$field' => $insert";
|
|
|
|
unset($value['type']);
|
|
|
|
$col .= join(', ', $schema->__values($value));
|
|
|
|
} else {
|
|
|
|
$col = "\t\t\t'indexes' => array(";
|
|
|
|
$props = array();
|
2008-02-14 04:48:40 +00:00
|
|
|
foreach ((array)$value as $key => $index) {
|
2008-01-05 19:31:29 +00:00
|
|
|
$props[] = "'{$key}' => array(".join(', ', $schema->__values($index)).")";
|
|
|
|
}
|
|
|
|
$col .= join(', ', $props);
|
|
|
|
}
|
|
|
|
$col .= ")";
|
|
|
|
$cols[] = $col;
|
|
|
|
}
|
|
|
|
$out .= join(",\n", $cols);
|
|
|
|
}
|
|
|
|
$out .= "\n\t\t\t);\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$records = join(",\n", $records);
|
|
|
|
$out .= "\tvar \$records = array(array(\n$records\n\t\t\t));\n";
|
|
|
|
$out .= "}\n";
|
|
|
|
$path = TESTS . DS . 'fixtures' . DS;
|
2008-03-09 01:05:27 +00:00
|
|
|
if (isset($this->plugin)) {
|
|
|
|
$pluginPath = 'plugins' . DS . Inflector::underscore($this->plugin) . DS;
|
|
|
|
$path = APP . $pluginPath . 'tests' . DS . 'fixtures' . DS;
|
|
|
|
}
|
2008-01-05 19:31:29 +00:00
|
|
|
$filename = Inflector::underscore($model).'_fixture.php';
|
2008-01-06 01:50:23 +00:00
|
|
|
$header = '$Id';
|
2008-01-05 19:31:29 +00:00
|
|
|
$content = "<?php \n/* SVN FILE: $header$ */\n/* ". $model ." Fixure generated on: " . date('Y-m-d H:m:s') . " : ". time() . "*/\n{$out}?>";
|
|
|
|
$this->out("\nBaking test fixture for $model...");
|
|
|
|
if ($this->createFile($path . $filename, $content)) {
|
|
|
|
return $records;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
|
|
|
?>
|