2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* The ModelTask handles creating and updating models files.
|
|
|
|
*
|
|
|
|
* Long description for file
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
|
|
* Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.console.libs.tasks
|
|
|
|
* @since CakePHP(tm) v 1.2
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Task class for creating and updating model files.
|
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.console.libs.tasks
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class ModelTask extends Shell {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Name of plugin
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $plugin = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-05 04:10:24 +00:00
|
|
|
/**
|
|
|
|
* Name of the db connection used.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $connection = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* path to MODELS directory
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $path = MODELS;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* tasks
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
2009-05-14 03:36:13 +00:00
|
|
|
var $tasks = array('DbConfig', 'Fixture', 'Test', 'Template');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-04-30 02:25:42 +00:00
|
|
|
/**
|
|
|
|
* Holds tables found on connection.
|
|
|
|
*
|
|
|
|
* @var array
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-04-30 02:25:42 +00:00
|
|
|
var $__tables = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-08 04:49:26 +00:00
|
|
|
/**
|
|
|
|
* Holds validation method map.
|
|
|
|
*
|
|
|
|
* @var array
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-05-08 04:49:26 +00:00
|
|
|
var $__validations = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Execution method always used for tasks
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function execute() {
|
2009-11-10 04:54:59 +00:00
|
|
|
App::import('Model', 'Model', false);
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (empty($this->args)) {
|
|
|
|
$this->__interactive();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->args[0])) {
|
2009-05-08 04:49:26 +00:00
|
|
|
$this->interactive = false;
|
|
|
|
if (!isset($this->connection)) {
|
|
|
|
$this->connection = 'default';
|
|
|
|
}
|
2009-04-28 03:15:28 +00:00
|
|
|
if (strtolower($this->args[0]) == 'all') {
|
|
|
|
return $this->all();
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
$model = Inflector::camelize($this->args[0]);
|
2009-05-08 04:49:26 +00:00
|
|
|
$object = $this->_getModelObject($model);
|
|
|
|
if ($this->bake($object, false)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->_checkUnitTest()) {
|
2009-07-15 14:00:35 +00:00
|
|
|
$this->bakeFixture($model);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->bakeTest($model);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-04-28 03:15:28 +00:00
|
|
|
/**
|
|
|
|
* Bake all models at once.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-04-28 03:15:28 +00:00
|
|
|
function all() {
|
2009-05-15 01:42:07 +00:00
|
|
|
$this->listAll($this->connection, false);
|
2009-07-15 14:00:35 +00:00
|
|
|
$unitTestExists = $this->_checkUnitTest();
|
2009-04-28 03:15:28 +00:00
|
|
|
foreach ($this->__tables as $table) {
|
|
|
|
$modelClass = Inflector::classify($table);
|
|
|
|
$this->out(sprintf(__('Baking %s', true), $modelClass));
|
2009-05-15 01:42:07 +00:00
|
|
|
$object = $this->_getModelObject($modelClass);
|
2009-07-15 14:00:35 +00:00
|
|
|
if ($this->bake($object, false) && $unitTestExists) {
|
|
|
|
$this->bakeFixture($modelClass);
|
|
|
|
$this->bakeTest($modelClass);
|
|
|
|
}
|
2009-04-28 03:15:28 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-08 04:49:26 +00:00
|
|
|
/**
|
|
|
|
* Get a model object for a class name.
|
|
|
|
*
|
|
|
|
* @param string $className Name of class you want model to be.
|
|
|
|
* @return object Model instance
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-11-16 14:24:52 +00:00
|
|
|
function &_getModelObject($className, $table = null) {
|
|
|
|
if (!$table) {
|
|
|
|
$table = Inflector::tableize($className);
|
|
|
|
}
|
|
|
|
$object =& new Model(array('name' => $className, 'table' => $table, 'ds' => $this->connection));
|
2009-05-08 04:49:26 +00:00
|
|
|
return $object;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-10 05:35:59 +00:00
|
|
|
/**
|
|
|
|
* Generate a key value list of options and a prompt.
|
2009-07-24 19:18:37 +00:00
|
|
|
*
|
2009-05-10 05:35:59 +00:00
|
|
|
* @param array $options Array of options to use for the selections. indexes must start at 0
|
|
|
|
* @param string $prompt Prompt to use for options list.
|
|
|
|
* @param integer $default The default option for the given prompt.
|
|
|
|
* @return result of user choice.
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-05-10 05:35:59 +00:00
|
|
|
function inOptions($options, $prompt = null, $default = null) {
|
|
|
|
$valid = false;
|
|
|
|
$max = count($options);
|
|
|
|
while (!$valid) {
|
|
|
|
foreach ($options as $i => $option) {
|
|
|
|
$this->out($i + 1 .'. ' . $option);
|
|
|
|
}
|
|
|
|
if (empty($prompt)) {
|
|
|
|
$prompt = __('Make a selection from the choices above', true);
|
|
|
|
}
|
|
|
|
$choice = $this->in($prompt, null, $default);
|
|
|
|
if (intval($choice) > 0 && intval($choice) <= $max) {
|
|
|
|
$valid = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $choice - 1;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Handles interactive baking
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function __interactive() {
|
|
|
|
$this->hr();
|
|
|
|
$this->out(sprintf("Bake Model\nPath: %s", $this->path));
|
|
|
|
$this->hr();
|
|
|
|
$this->interactive = true;
|
|
|
|
|
|
|
|
$primaryKey = 'id';
|
2009-05-05 04:10:24 +00:00
|
|
|
$validate = $associations = array();
|
|
|
|
|
|
|
|
if (empty($this->connection)) {
|
|
|
|
$this->connection = $this->DbConfig->getConfig();
|
|
|
|
}
|
|
|
|
$currentModelName = $this->getName();
|
|
|
|
$useTable = $this->getTable($currentModelName);
|
|
|
|
$db =& ConnectionManager::getDataSource($this->connection);
|
2009-04-29 01:20:34 +00:00
|
|
|
$fullTableName = $db->fullTableName($useTable);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (in_array($useTable, $this->__tables)) {
|
2009-05-05 04:10:24 +00:00
|
|
|
$tempModel = new Model(array('name' => $currentModelName, 'table' => $useTable, 'ds' => $this->connection));
|
2008-05-30 11:40:08 +00:00
|
|
|
$fields = $tempModel->schema();
|
|
|
|
if (!array_key_exists('id', $fields)) {
|
2009-05-05 04:43:29 +00:00
|
|
|
$primaryKey = $this->findPrimaryKey($fields);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-09 03:30:29 +00:00
|
|
|
} else {
|
|
|
|
$this->err(sprintf(__('Table %s does not exist, cannot bake a model without a table.', true), $useTable));
|
|
|
|
$this->_stop();
|
|
|
|
return false;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-08 13:10:18 +00:00
|
|
|
$displayField = $tempModel->hasField(array('name', 'title'));
|
|
|
|
if (!$displayField) {
|
|
|
|
$displayField = $this->findDisplayField($tempModel->schema());
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-05-09 05:00:14 +00:00
|
|
|
$prompt = __("Would you like to supply validation criteria \nfor the fields in your model?", true);
|
2009-05-05 04:43:29 +00:00
|
|
|
$wannaDoValidation = $this->in($prompt, array('y','n'), 'y');
|
|
|
|
if (array_search($useTable, $this->__tables) !== false && strtolower($wannaDoValidation) == 'y') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$validate = $this->doValidation($tempModel);
|
|
|
|
}
|
|
|
|
|
2009-05-09 05:00:14 +00:00
|
|
|
$prompt = __("Would you like to define model associations\n(hasMany, hasOne, belongsTo, etc.)?", true);
|
2009-05-05 04:43:29 +00:00
|
|
|
$wannaDoAssoc = $this->in($prompt, array('y','n'), 'y');
|
|
|
|
if (strtolower($wannaDoAssoc) == 'y') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$associations = $this->doAssociations($tempModel);
|
|
|
|
}
|
|
|
|
|
2009-09-26 21:08:37 +00:00
|
|
|
$this->out();
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->hr();
|
|
|
|
$this->out(__('The following Model will be created:', true));
|
|
|
|
$this->hr();
|
|
|
|
$this->out("Name: " . $currentModelName);
|
|
|
|
|
2009-05-05 04:10:24 +00:00
|
|
|
if ($this->connection !== 'default') {
|
2009-05-15 02:36:04 +00:00
|
|
|
$this->out(sprintf(__("DB Config: %s", true), $this->connection));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if ($fullTableName !== Inflector::tableize($currentModelName)) {
|
2009-05-05 04:43:29 +00:00
|
|
|
$this->out(sprintf(__("DB Table: %s", true), $fullTableName));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if ($primaryKey != 'id') {
|
2009-05-05 04:43:29 +00:00
|
|
|
$this->out(sprintf(__("Primary Key: %s", true), $primaryKey));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if (!empty($validate)) {
|
2009-05-05 04:43:29 +00:00
|
|
|
$this->out(sprintf(__("Validation: %s", true), print_r($validate, true)));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if (!empty($associations)) {
|
2009-05-05 04:43:29 +00:00
|
|
|
$this->out(__("Associations:", true));
|
|
|
|
$assocKeys = array('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany');
|
|
|
|
foreach ($assocKeys as $assocKey) {
|
2009-05-08 05:09:19 +00:00
|
|
|
$this->_printAssociation($currentModelName, $assocKey, $associations);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-05-05 04:43:29 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->hr();
|
|
|
|
$looksGood = $this->in(__('Look okay?', true), array('y','n'), 'y');
|
2009-07-09 03:30:29 +00:00
|
|
|
|
2009-05-05 04:43:29 +00:00
|
|
|
if (strtolower($looksGood) == 'y') {
|
2009-07-09 02:06:42 +00:00
|
|
|
$vars = compact('associations', 'validate', 'primaryKey', 'useTable', 'displayField');
|
|
|
|
$vars['useDbConfig'] = $this->connection;
|
|
|
|
if ($this->bake($currentModelName, $vars)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->_checkUnitTest()) {
|
2009-05-15 01:42:07 +00:00
|
|
|
$this->bakeFixture($currentModelName, $useTable);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->bakeTest($currentModelName, $useTable, $associations);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-05 04:43:29 +00:00
|
|
|
/**
|
|
|
|
* Print out all the associations of a particular type
|
|
|
|
*
|
|
|
|
* @param string $modelName Name of the model relations belong to.
|
|
|
|
* @param string $type Name of association you want to see. i.e. 'belongsTo'
|
|
|
|
* @param string $associations Collection of associations.
|
2009-05-08 05:09:19 +00:00
|
|
|
* @access protected
|
2009-05-05 04:43:29 +00:00
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-05-08 05:09:19 +00:00
|
|
|
function _printAssociation($modelName, $type, $associations) {
|
2009-05-05 04:43:29 +00:00
|
|
|
if (!empty($associations[$type])) {
|
|
|
|
for ($i = 0; $i < count($associations[$type]); $i++) {
|
|
|
|
$out = "\t" . $modelName . ' ' . $type . ' ' . $associations[$type][$i]['alias'];
|
|
|
|
$this->out($out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-05 04:43:29 +00:00
|
|
|
/**
|
|
|
|
* Finds a primary Key in a list of fields.
|
|
|
|
*
|
|
|
|
* @param array $fields Array of fields that might have a primary key.
|
|
|
|
* @return string Name of field that is a primary key.
|
2009-05-08 04:49:26 +00:00
|
|
|
* @access public
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-05-05 04:43:29 +00:00
|
|
|
function findPrimaryKey($fields) {
|
|
|
|
foreach ($fields as $name => $field) {
|
|
|
|
if (isset($field['key']) && $field['key'] == 'primary') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->in(__('What is the primaryKey?', true), null, $name);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-07-08 13:10:18 +00:00
|
|
|
/**
|
|
|
|
* interact with the user to find the displayField value for a model.
|
|
|
|
*
|
|
|
|
* @param array $fields Array of fields to look for and choose as a displayField
|
|
|
|
* @return mixed Name of field to use for displayField or false if the user declines to choose
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-07-08 13:10:18 +00:00
|
|
|
function findDisplayField($fields) {
|
|
|
|
$fieldNames = array_keys($fields);
|
|
|
|
$prompt = __("A displayField could not be automatically detected\nwould you like to choose one?", true);
|
|
|
|
$continue = $this->in($prompt, array('y', 'n'));
|
|
|
|
if (strtolower($continue) == 'n') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$prompt = __('Choose a field from the options above:', true);
|
|
|
|
$choice = $this->inOptions($fieldNames, $prompt);
|
|
|
|
return $fieldNames[$choice];
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-05-08 04:49:26 +00:00
|
|
|
* Handles Generation and user interaction for creating validation.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-07-08 13:10:18 +00:00
|
|
|
* @param object $model Model to have validations generated for.
|
|
|
|
* @return array $validate Array of user selected validations.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2009-05-05 04:43:29 +00:00
|
|
|
function doValidation(&$model) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!is_object($model)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$fields = $model->schema();
|
|
|
|
|
|
|
|
if (empty($fields)) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-05-08 04:49:26 +00:00
|
|
|
$validate = array();
|
|
|
|
$this->initValidations();
|
|
|
|
foreach ($fields as $fieldName => $field) {
|
|
|
|
$validation = $this->fieldValidation($fieldName, $field, $model->primaryKey);
|
|
|
|
if (!empty($validation)) {
|
|
|
|
$validate[$fieldName] = $validation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $validate;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-08 04:49:26 +00:00
|
|
|
/**
|
2009-07-24 19:18:37 +00:00
|
|
|
* Populate the __validations array
|
2009-05-08 04:49:26 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-05-08 04:49:26 +00:00
|
|
|
function initValidations() {
|
|
|
|
$options = $choices = array();
|
2008-05-30 11:40:08 +00:00
|
|
|
if (class_exists('Validation')) {
|
2009-10-12 01:35:06 +00:00
|
|
|
$parent = get_class_methods(get_parent_class('Validation'));
|
2009-10-07 04:51:55 +00:00
|
|
|
$options = get_class_methods('Validation');
|
2009-10-12 01:35:06 +00:00
|
|
|
$options = array_diff($options, $parent);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-05-08 04:49:26 +00:00
|
|
|
sort($options);
|
|
|
|
$default = 1;
|
|
|
|
foreach ($options as $key => $option) {
|
|
|
|
if ($option{0} != '_' && strtolower($option) != 'getinstance') {
|
|
|
|
$choices[$default] = strtolower($option);
|
|
|
|
$default++;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-05-08 04:49:26 +00:00
|
|
|
}
|
|
|
|
$this->__validations = $choices;
|
|
|
|
return $choices;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-08 04:49:26 +00:00
|
|
|
/**
|
|
|
|
* Does individual field validation handling.
|
|
|
|
*
|
|
|
|
* @param string $fieldName Name of field to be validated.
|
|
|
|
* @param array $metaData metadata for field
|
|
|
|
* @return array Array of validation for the field.
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-05-08 04:49:26 +00:00
|
|
|
function fieldValidation($fieldName, $metaData, $primaryKey = 'id') {
|
|
|
|
$defaultChoice = count($this->__validations);
|
2009-05-09 04:35:03 +00:00
|
|
|
$validate = $alreadyChosen = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-08 05:09:19 +00:00
|
|
|
$anotherValidator = 'y';
|
|
|
|
while ($anotherValidator == 'y') {
|
|
|
|
if ($this->interactive) {
|
2009-09-26 21:08:37 +00:00
|
|
|
$this->out();
|
2009-05-08 05:09:19 +00:00
|
|
|
$this->out(sprintf(__('Field: %s', true), $fieldName));
|
|
|
|
$this->out(sprintf(__('Type: %s', true), $metaData['type']));
|
|
|
|
$this->hr();
|
|
|
|
$this->out(__('Please select one of the following validation options:', true));
|
|
|
|
$this->hr();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-08 05:09:19 +00:00
|
|
|
$prompt = '';
|
|
|
|
for ($i = 1; $i < $defaultChoice; $i++) {
|
|
|
|
$prompt .= $i . ' - ' . $this->__validations[$i] . "\n";
|
|
|
|
}
|
|
|
|
$prompt .= sprintf(__("%s - Do not do any validation on this field.\n", true), $defaultChoice);
|
|
|
|
$prompt .= __("... or enter in a valid regex validation string.\n", true);
|
|
|
|
|
|
|
|
$methods = array_flip($this->__validations);
|
|
|
|
$guess = $defaultChoice;
|
|
|
|
if ($metaData['null'] != 1 && !in_array($fieldName, array($primaryKey, 'created', 'modified', 'updated'))) {
|
|
|
|
if ($fieldName == 'email') {
|
|
|
|
$guess = $methods['email'];
|
|
|
|
} elseif ($metaData['type'] == 'string') {
|
|
|
|
$guess = $methods['notempty'];
|
|
|
|
} elseif ($metaData['type'] == 'integer') {
|
|
|
|
$guess = $methods['numeric'];
|
|
|
|
} elseif ($metaData['type'] == 'boolean') {
|
2009-07-30 02:20:07 +00:00
|
|
|
$guess = $methods['boolean'];
|
2009-09-22 22:49:30 +00:00
|
|
|
} elseif ($metaData['type'] == 'date') {
|
2009-05-08 05:09:19 +00:00
|
|
|
$guess = $methods['date'];
|
|
|
|
} elseif ($metaData['type'] == 'time') {
|
|
|
|
$guess = $methods['time'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->interactive === true) {
|
|
|
|
$choice = $this->in($prompt, null, $guess);
|
2009-05-09 04:35:03 +00:00
|
|
|
if (in_array($choice, $alreadyChosen)) {
|
2009-05-09 05:00:14 +00:00
|
|
|
$this->out(__("You have already chosen that validation rule,\nplease choose again", true));
|
2009-05-09 04:35:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
2009-05-08 05:09:19 +00:00
|
|
|
$alreadyChosen[] = $choice;
|
|
|
|
} else {
|
|
|
|
$choice = $guess;
|
|
|
|
}
|
|
|
|
$validatorName = $this->__validations[$choice];
|
|
|
|
if ($choice != $defaultChoice) {
|
|
|
|
if (is_numeric($choice) && isset($this->__validations[$choice])) {
|
|
|
|
$validate[$validatorName] = $this->__validations[$choice];
|
|
|
|
} else {
|
|
|
|
$validate[$validatorName] = $choice;
|
|
|
|
}
|
|
|
|
}
|
2009-05-09 04:35:03 +00:00
|
|
|
if ($this->interactive == true && $choice != $defaultChoice) {
|
2009-05-08 05:09:19 +00:00
|
|
|
$anotherValidator = $this->in(__('Would you like to add another validation rule?', true), array('y', 'n'), 'n');
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2009-05-08 05:09:19 +00:00
|
|
|
$anotherValidator = 'n';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $validate;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Handles associations
|
|
|
|
*
|
|
|
|
* @param object $model
|
|
|
|
* @return array $assocaitons
|
|
|
|
* @access public
|
|
|
|
*/
|
2009-05-05 04:43:29 +00:00
|
|
|
function doAssociations(&$model) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!is_object($model)) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-16 12:52:33 +00:00
|
|
|
if ($this->interactive === true) {
|
|
|
|
$this->out(__('One moment while the associations are detected.', true));
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$fields = $model->schema();
|
|
|
|
if (empty($fields)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-05-05 04:43:29 +00:00
|
|
|
$associations = array(
|
|
|
|
'belongsTo' => array(), 'hasMany' => array(), 'hasOne'=> array(), 'hasAndBelongsToMany' => array()
|
|
|
|
);
|
2008-09-07 13:03:51 +00:00
|
|
|
$possibleKeys = array();
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-05-10 01:28:51 +00:00
|
|
|
$associations = $this->findBelongsTo($model, $associations);
|
|
|
|
$associations = $this->findHasOneAndMany($model, $associations);
|
|
|
|
$associations = $this->findHasAndBelongsToMany($model, $associations);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-05-05 04:43:29 +00:00
|
|
|
if ($this->interactive !== true) {
|
2008-05-30 11:40:08 +00:00
|
|
|
unset($associations['hasOne']);
|
|
|
|
}
|
|
|
|
|
2009-05-05 04:43:29 +00:00
|
|
|
if ($this->interactive === true) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->hr();
|
|
|
|
if (empty($associations)) {
|
|
|
|
$this->out(__('None found.', true));
|
|
|
|
} else {
|
|
|
|
$this->out(__('Please confirm the following associations:', true));
|
|
|
|
$this->hr();
|
2009-05-10 03:32:22 +00:00
|
|
|
$associations = $this->confirmAssociations($model, $associations);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-05-10 05:04:40 +00:00
|
|
|
$associations = $this->doMoreAssociations($model, $associations);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return $associations;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-09 06:20:46 +00:00
|
|
|
/**
|
|
|
|
* Find belongsTo relations and add them to the associations list.
|
|
|
|
*
|
|
|
|
* @param object $model Model instance of model being generated.
|
|
|
|
* @param array $associations Array of inprogress associations
|
|
|
|
* @return array $associations with belongsTo added in.
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-05-10 01:28:51 +00:00
|
|
|
function findBelongsTo(&$model, $associations) {
|
2009-05-09 06:20:46 +00:00
|
|
|
$fields = $model->schema();
|
|
|
|
foreach ($fields as $fieldName => $field) {
|
|
|
|
$offset = strpos($fieldName, '_id');
|
2009-05-10 03:32:22 +00:00
|
|
|
if ($fieldName != $model->primaryKey && $fieldName != 'parent_id' && $offset !== false) {
|
2009-05-09 06:20:46 +00:00
|
|
|
$tmpModelName = $this->_modelNameFromKey($fieldName);
|
2009-05-09 06:22:29 +00:00
|
|
|
$associations['belongsTo'][] = array(
|
2009-05-09 06:20:46 +00:00
|
|
|
'alias' => $tmpModelName,
|
|
|
|
'className' => $tmpModelName,
|
|
|
|
'foreignKey' => $fieldName,
|
|
|
|
);
|
2009-05-10 03:32:22 +00:00
|
|
|
} elseif ($fieldName == 'parent_id') {
|
|
|
|
$associations['belongsTo'][] = array(
|
|
|
|
'alias' => 'Parent' . $model->name,
|
|
|
|
'className' => $model->name,
|
|
|
|
'foreignKey' => $fieldName,
|
|
|
|
);
|
2009-05-09 06:20:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $associations;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-09 06:20:46 +00:00
|
|
|
/**
|
|
|
|
* Find the hasOne and HasMany relations and add them to associations list
|
|
|
|
*
|
2009-07-24 19:18:37 +00:00
|
|
|
* @param object $model Model instance being generated
|
2009-05-09 06:20:46 +00:00
|
|
|
* @param array $associations Array of inprogress associations
|
|
|
|
* @return array $associations with hasOne and hasMany added in.
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-05-10 01:28:51 +00:00
|
|
|
function findHasOneAndMany(&$model, $associations) {
|
2009-05-09 06:20:46 +00:00
|
|
|
$foreignKey = $this->_modelKey($model->name);
|
|
|
|
foreach ($this->__tables as $otherTable) {
|
2009-11-16 14:24:52 +00:00
|
|
|
$tempOtherModel = $this->_getModelObject($this->_modelName($otherTable), $otherTable);
|
2009-05-09 06:20:46 +00:00
|
|
|
$modelFieldsTemp = $tempOtherModel->schema();
|
|
|
|
|
2009-05-10 01:28:51 +00:00
|
|
|
$pattern = '/_' . preg_quote($model->table, '/') . '|' . preg_quote($model->table, '/') . '_/';
|
|
|
|
$possibleJoinTable = preg_match($pattern , $otherTable);
|
2009-05-10 03:32:22 +00:00
|
|
|
if ($possibleJoinTable == true) {
|
|
|
|
continue;
|
|
|
|
}
|
2009-05-09 06:20:46 +00:00
|
|
|
foreach ($modelFieldsTemp as $fieldName => $field) {
|
2009-05-10 03:32:22 +00:00
|
|
|
$assoc = false;
|
|
|
|
if ($fieldName != $model->primaryKey && $fieldName == $foreignKey) {
|
2009-05-09 06:20:46 +00:00
|
|
|
$assoc = array(
|
|
|
|
'alias' => $tempOtherModel->name,
|
|
|
|
'className' => $tempOtherModel->name,
|
|
|
|
'foreignKey' => $fieldName
|
|
|
|
);
|
2009-05-10 03:32:22 +00:00
|
|
|
} elseif ($otherTable == $model->table && $fieldName == 'parent_id') {
|
|
|
|
$assoc = array(
|
|
|
|
'alias' => 'Child' . $model->name,
|
|
|
|
'className' => $model->name,
|
|
|
|
'foreignKey' => $fieldName
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if ($assoc) {
|
2009-05-09 06:20:46 +00:00
|
|
|
$associations['hasOne'][] = $assoc;
|
|
|
|
$associations['hasMany'][] = $assoc;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-09 06:20:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $associations;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-09 06:20:46 +00:00
|
|
|
/**
|
|
|
|
* Find the hasAndBelongsToMany relations and add them to associations list
|
|
|
|
*
|
2009-07-24 19:18:37 +00:00
|
|
|
* @param object $model Model instance being generated
|
2009-05-09 06:20:46 +00:00
|
|
|
* @param array $associations Array of inprogress associations
|
|
|
|
* @return array $associations with hasAndBelongsToMany added in.
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-05-10 01:28:51 +00:00
|
|
|
function findHasAndBelongsToMany(&$model, $associations) {
|
2009-05-09 06:20:46 +00:00
|
|
|
$foreignKey = $this->_modelKey($model->name);
|
|
|
|
foreach ($this->__tables as $otherTable) {
|
2009-11-16 14:24:52 +00:00
|
|
|
$tempOtherModel = $this->_getModelObject($this->_modelName($otherTable), $otherTable);
|
2009-05-09 06:20:46 +00:00
|
|
|
$modelFieldsTemp = $tempOtherModel->schema();
|
|
|
|
|
|
|
|
$offset = strpos($otherTable, $model->table . '_');
|
|
|
|
$otherOffset = strpos($otherTable, '_' . $model->table);
|
|
|
|
|
|
|
|
if ($offset !== false) {
|
|
|
|
$offset = strlen($model->table . '_');
|
|
|
|
$habtmName = $this->_modelName(substr($otherTable, $offset));
|
2009-05-09 06:22:29 +00:00
|
|
|
$associations['hasAndBelongsToMany'][] = array(
|
2009-05-09 06:20:46 +00:00
|
|
|
'alias' => $habtmName,
|
|
|
|
'className' => $habtmName,
|
|
|
|
'foreignKey' => $foreignKey,
|
|
|
|
'associationForeignKey' => $this->_modelKey($habtmName),
|
|
|
|
'joinTable' => $otherTable
|
|
|
|
);
|
|
|
|
} elseif ($otherOffset !== false) {
|
|
|
|
$habtmName = $this->_modelName(substr($otherTable, 0, $otherOffset));
|
2009-05-09 06:22:29 +00:00
|
|
|
$associations['hasAndBelongsToMany'][] = array(
|
2009-05-09 06:20:46 +00:00
|
|
|
'alias' => $habtmName,
|
|
|
|
'className' => $habtmName,
|
|
|
|
'foreignKey' => $foreignKey,
|
|
|
|
'associationForeignKey' => $this->_modelKey($habtmName),
|
|
|
|
'joinTable' => $otherTable
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $associations;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-10 03:32:22 +00:00
|
|
|
/**
|
|
|
|
* Interact with the user and confirm associations.
|
|
|
|
*
|
|
|
|
* @param array $model Temporary Model instance.
|
|
|
|
* @param array $associations Array of associations to be confirmed.
|
|
|
|
* @return array Array of confirmed associations
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-05-10 03:32:22 +00:00
|
|
|
function confirmAssociations(&$model, $associations) {
|
|
|
|
foreach ($associations as $type => $settings) {
|
|
|
|
if (!empty($associations[$type])) {
|
|
|
|
$count = count($associations[$type]);
|
|
|
|
$response = 'y';
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
|
|
$prompt = "{$model->name} {$type} {$associations[$type][$i]['alias']}";
|
|
|
|
$response = $this->in("{$prompt}?", array('y','n'), 'y');
|
|
|
|
|
2009-08-28 02:46:00 +00:00
|
|
|
if ('n' == strtolower($response)) {
|
2009-05-10 03:32:22 +00:00
|
|
|
unset($associations[$type][$i]);
|
|
|
|
} elseif ($type == 'hasMany') {
|
|
|
|
unset($associations['hasOne'][$i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$associations[$type] = array_merge($associations[$type]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $associations;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-10 05:04:40 +00:00
|
|
|
/**
|
|
|
|
* Interact with the user and generate additional non-conventional associations
|
|
|
|
*
|
|
|
|
* @param object $model Temporary model instance
|
|
|
|
* @param array $associations Array of associations.
|
|
|
|
* @return array Array of associations.
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-05-10 05:04:40 +00:00
|
|
|
function doMoreAssociations($model, $associations) {
|
|
|
|
$prompt = __('Would you like to define some additional model associations?', true);
|
|
|
|
$wannaDoMoreAssoc = $this->in($prompt, array('y','n'), 'n');
|
|
|
|
$possibleKeys = $this->_generatePossibleKeys();
|
2009-08-28 02:46:00 +00:00
|
|
|
while (strtolower($wannaDoMoreAssoc) == 'y') {
|
2009-05-10 05:35:59 +00:00
|
|
|
$assocs = array('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany');
|
2009-05-10 05:04:40 +00:00
|
|
|
$this->out(__('What is the association type?', true));
|
2009-05-10 05:35:59 +00:00
|
|
|
$assocType = intval($this->inOptions($assocs, __('Enter a number',true)));
|
|
|
|
|
2009-05-10 05:04:40 +00:00
|
|
|
$this->out(__("For the following options be very careful to match your setup exactly.\nAny spelling mistakes will cause errors.", true));
|
|
|
|
$this->hr();
|
|
|
|
|
|
|
|
$alias = $this->in(__('What is the alias for this association?', true));
|
|
|
|
$className = $this->in(sprintf(__('What className will %s use?', true), $alias), null, $alias );
|
|
|
|
$suggestedForeignKey = null;
|
|
|
|
|
2009-05-10 05:35:59 +00:00
|
|
|
if ($assocType == 0) {
|
2009-05-10 05:04:40 +00:00
|
|
|
$showKeys = $possibleKeys[$model->table];
|
|
|
|
$suggestedForeignKey = $this->_modelKey($alias);
|
|
|
|
} else {
|
|
|
|
$otherTable = Inflector::tableize($className);
|
|
|
|
if (in_array($otherTable, $this->__tables)) {
|
2009-05-10 05:35:59 +00:00
|
|
|
if ($assocType < 3) {
|
2009-05-10 05:04:40 +00:00
|
|
|
$showKeys = $possibleKeys[$otherTable];
|
|
|
|
} else {
|
|
|
|
$showKeys = null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$otherTable = $this->in(__('What is the table for this model?', true));
|
|
|
|
$showKeys = $possibleKeys[$otherTable];
|
|
|
|
}
|
|
|
|
$suggestedForeignKey = $this->_modelKey($model->name);
|
|
|
|
}
|
|
|
|
if (!empty($showKeys)) {
|
|
|
|
$this->out(__('A helpful List of possible keys', true));
|
2009-05-10 05:35:59 +00:00
|
|
|
$foreignKey = $this->inOptions($showKeys, __('What is the foreignKey?', true));
|
|
|
|
$foreignKey = $showKeys[intval($foreignKey)];
|
2009-05-10 05:04:40 +00:00
|
|
|
}
|
|
|
|
if (!isset($foreignKey)) {
|
|
|
|
$foreignKey = $this->in(__('What is the foreignKey? Specify your own.', true), null, $suggestedForeignKey);
|
|
|
|
}
|
2009-05-10 05:35:59 +00:00
|
|
|
if ($assocType == 3) {
|
2009-05-10 05:04:40 +00:00
|
|
|
$associationForeignKey = $this->in(__('What is the associationForeignKey?', true), null, $this->_modelKey($model->name));
|
|
|
|
$joinTable = $this->in(__('What is the joinTable?', true));
|
|
|
|
}
|
|
|
|
$associations[$assocs[$assocType]] = array_values((array)$associations[$assocs[$assocType]]);
|
|
|
|
$count = count($associations[$assocs[$assocType]]);
|
|
|
|
$i = ($count > 0) ? $count : 0;
|
|
|
|
$associations[$assocs[$assocType]][$i]['alias'] = $alias;
|
|
|
|
$associations[$assocs[$assocType]][$i]['className'] = $className;
|
|
|
|
$associations[$assocs[$assocType]][$i]['foreignKey'] = $foreignKey;
|
2009-05-10 05:35:59 +00:00
|
|
|
if ($assocType == 3) {
|
2009-05-10 05:04:40 +00:00
|
|
|
$associations[$assocs[$assocType]][$i]['associationForeignKey'] = $associationForeignKey;
|
|
|
|
$associations[$assocs[$assocType]][$i]['joinTable'] = $joinTable;
|
|
|
|
}
|
|
|
|
$wannaDoMoreAssoc = $this->in(__('Define another association?', true), array('y','n'), 'y');
|
|
|
|
}
|
|
|
|
return $associations;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-10 05:04:40 +00:00
|
|
|
/**
|
|
|
|
* Finds all possible keys to use on custom associations.
|
|
|
|
*
|
|
|
|
* @return array array of tables and possible keys
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-05-10 05:04:40 +00:00
|
|
|
function _generatePossibleKeys() {
|
|
|
|
$possible = array();
|
|
|
|
foreach ($this->__tables as $otherTable) {
|
|
|
|
$tempOtherModel = & new Model(array('table' => $otherTable, 'ds' => $this->connection));
|
|
|
|
$modelFieldsTemp = $tempOtherModel->schema();
|
|
|
|
foreach ($modelFieldsTemp as $fieldName => $field) {
|
|
|
|
if ($field['type'] == 'integer' || $field['type'] == 'string') {
|
|
|
|
$possible[$otherTable][] = $fieldName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $possible;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Assembles and writes a Model file.
|
|
|
|
*
|
|
|
|
* @param mixed $name Model name or object
|
2009-07-09 02:06:42 +00:00
|
|
|
* @param mixed $data if array and $name is not an object assume bake data, otherwise boolean.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2009-07-09 02:06:42 +00:00
|
|
|
function bake($name, $data = array()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_object($name)) {
|
2009-07-09 02:06:42 +00:00
|
|
|
if ($data == false) {
|
|
|
|
$data = $associations = array();
|
|
|
|
$data['associations'] = $this->doAssociations($name, $associations);
|
|
|
|
$data['validate'] = $this->doValidation($name);
|
|
|
|
}
|
|
|
|
$data['primaryKey'] = $name->primaryKey;
|
|
|
|
$data['useTable'] = $name->table;
|
|
|
|
$data['useDbConfig'] = $name->useDbConfig;
|
|
|
|
$data['name'] = $name = $name->name;
|
|
|
|
} else {
|
|
|
|
$data['name'] = $name;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
$defaults = array('associations' => array(), 'validate' => array(), 'primaryKey' => 'id',
|
2009-07-09 02:06:42 +00:00
|
|
|
'useTable' => null, 'useDbConfig' => 'default', 'displayField' => null);
|
|
|
|
$data = array_merge($defaults, $data);
|
2009-05-14 03:37:21 +00:00
|
|
|
|
2009-07-09 02:06:42 +00:00
|
|
|
$this->Template->set($data);
|
2009-06-17 02:29:48 +00:00
|
|
|
$this->Template->set('plugin', Inflector::camelize($this->plugin));
|
2009-07-01 04:50:38 +00:00
|
|
|
$out = $this->Template->generate('classes', 'model');
|
2009-06-07 00:37:01 +00:00
|
|
|
|
|
|
|
$path = $this->path;
|
|
|
|
if (isset($this->plugin)) {
|
|
|
|
$path = $this->_pluginPath($this->plugin) . 'models' . DS;
|
|
|
|
}
|
|
|
|
$filename = $path . Inflector::underscore($name) . '.php';
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->out("\nBaking model class for $name...");
|
2009-05-14 03:36:13 +00:00
|
|
|
$this->createFile($filename, $out);
|
|
|
|
return $out;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Assembles and writes a unit test file
|
|
|
|
*
|
|
|
|
* @param string $className Model class name
|
|
|
|
* @access private
|
|
|
|
*/
|
2009-07-15 14:00:35 +00:00
|
|
|
function bakeTest($className) {
|
2009-05-15 01:42:07 +00:00
|
|
|
$this->Test->plugin = $this->plugin;
|
|
|
|
$this->Test->connection = $this->connection;
|
|
|
|
return $this->Test->bake('Model', $className);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* outputs the a list of possible models or controllers from database
|
|
|
|
*
|
|
|
|
* @param string $useDbConfig Database configuration name
|
|
|
|
* @access public
|
|
|
|
*/
|
2009-05-15 03:00:29 +00:00
|
|
|
function listAll($useDbConfig = null) {
|
|
|
|
$this->__tables = $this->getAllTables($useDbConfig);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-05-15 03:00:29 +00:00
|
|
|
if ($this->interactive === true) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->out(__('Possible Models based on your current database:', true));
|
|
|
|
$this->_modelNames = array();
|
2009-05-15 03:00:29 +00:00
|
|
|
$count = count($this->__tables);
|
2008-05-30 11:40:08 +00:00
|
|
|
for ($i = 0; $i < $count; $i++) {
|
2009-05-15 03:00:29 +00:00
|
|
|
$this->_modelNames[] = $this->_modelName($this->__tables[$i]);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->out($i + 1 . ". " . $this->_modelNames[$i]);
|
|
|
|
}
|
|
|
|
}
|
2009-04-30 02:50:43 +00:00
|
|
|
return $this->__tables;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-04-29 01:20:34 +00:00
|
|
|
/**
|
|
|
|
* Interact with the user to determine the table name of a particular model
|
2009-07-24 19:18:37 +00:00
|
|
|
*
|
2009-04-29 01:20:34 +00:00
|
|
|
* @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
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-05-05 04:10:24 +00:00
|
|
|
function getTable($modelName, $useDbConfig = null) {
|
|
|
|
if (!isset($useDbConfig)) {
|
|
|
|
$useDbConfig = $this->connection;
|
|
|
|
}
|
2009-11-04 18:21:05 +00:00
|
|
|
App::import('Model', 'ConnectionManager', false);
|
|
|
|
|
2009-04-29 01:20:34 +00:00
|
|
|
$db =& ConnectionManager::getDataSource($useDbConfig);
|
|
|
|
$useTable = Inflector::tableize($modelName);
|
|
|
|
$fullTableName = $db->fullTableName($useTable, false);
|
|
|
|
$tableIsGood = false;
|
|
|
|
|
|
|
|
if (array_search($useTable, $this->__tables) === false) {
|
2009-09-26 21:08:37 +00:00
|
|
|
$this->out();
|
2009-05-09 05:00:14 +00:00
|
|
|
$this->out(sprintf(__("Given your model named '%s',\nCake would expect a database table named '%s'", true), $modelName, $fullTableName));
|
2009-04-29 01:20:34 +00:00
|
|
|
$tableIsGood = $this->in(__('Do you want to use this table?', true), array('y','n'), 'y');
|
|
|
|
}
|
2009-05-10 16:43:38 +00:00
|
|
|
if (strtolower($tableIsGood) == 'n') {
|
2009-05-24 03:25:14 +00:00
|
|
|
$useTable = $this->in(__('What is the name of the table?', true));
|
2009-04-29 01:20:34 +00:00
|
|
|
}
|
|
|
|
return $useTable;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-15 03:00:29 +00:00
|
|
|
/**
|
|
|
|
* Get an Array of all the tables in the supplied connection
|
|
|
|
* will halt the script if no tables are found.
|
2009-07-24 19:18:37 +00:00
|
|
|
*
|
2009-05-15 03:00:29 +00:00
|
|
|
* @param string $useDbConfig Connection name to scan.
|
|
|
|
* @return array Array of tables in the database.
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-05-15 03:00:29 +00:00
|
|
|
function getAllTables($useDbConfig = null) {
|
|
|
|
if (!isset($useDbConfig)) {
|
|
|
|
$useDbConfig = $this->connection;
|
|
|
|
}
|
2009-11-04 18:21:05 +00:00
|
|
|
App::import('Model', 'ConnectionManager', false);
|
|
|
|
|
2009-05-15 03:00:29 +00:00
|
|
|
$tables = array();
|
|
|
|
$db =& ConnectionManager::getDataSource($useDbConfig);
|
|
|
|
$usePrefix = empty($db->config['prefix']) ? '' : $db->config['prefix'];
|
|
|
|
if ($usePrefix) {
|
|
|
|
foreach ($db->listSources() as $table) {
|
|
|
|
if (!strncmp($table, $usePrefix, strlen($usePrefix))) {
|
|
|
|
$tables[] = substr($table, strlen($usePrefix));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$tables = $db->listSources();
|
|
|
|
}
|
|
|
|
if (empty($tables)) {
|
|
|
|
$this->err(__('Your database does not have any tables.', true));
|
|
|
|
$this->_stop();
|
|
|
|
}
|
|
|
|
return $tables;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Forces the user to specify the model he wants to bake, and returns the selected model name.
|
|
|
|
*
|
|
|
|
* @return string the model name
|
|
|
|
* @access public
|
|
|
|
*/
|
2009-05-05 04:10:24 +00:00
|
|
|
function getName($useDbConfig = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->listAll($useDbConfig);
|
|
|
|
|
|
|
|
$enteredModel = '';
|
|
|
|
|
|
|
|
while ($enteredModel == '') {
|
2009-05-09 05:00:14 +00:00
|
|
|
$enteredModel = $this->in(__("Enter a number from the list above,\ntype in the name of another model, or 'q' to exit", true), null, 'q');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($enteredModel === 'q') {
|
|
|
|
$this->out(__("Exit", true));
|
2008-06-04 19:04:58 +00:00
|
|
|
$this->_stop();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($enteredModel == '' || intval($enteredModel) > count($this->_modelNames)) {
|
2009-05-09 05:00:14 +00:00
|
|
|
$this->err(__("The model name you supplied was empty,\nor the number you selected was not an option. Please try again.", true));
|
2008-05-30 11:40:08 +00:00
|
|
|
$enteredModel = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (intval($enteredModel) > 0 && intval($enteredModel) <= count($this->_modelNames)) {
|
|
|
|
$currentModelName = $this->_modelNames[intval($enteredModel) - 1];
|
|
|
|
} else {
|
|
|
|
$currentModelName = $enteredModel;
|
|
|
|
}
|
|
|
|
return $currentModelName;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Displays help contents
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function help() {
|
|
|
|
$this->hr();
|
|
|
|
$this->out("Usage: cake bake model <arg1>");
|
|
|
|
$this->hr();
|
2009-10-04 21:59:12 +00:00
|
|
|
$this->out('Arguments:');
|
|
|
|
$this->out();
|
|
|
|
$this->out("<name>");
|
|
|
|
$this->out("\tName of the model to bake. Can use Plugin.name");
|
|
|
|
$this->out("\tas a shortcut for plugin baking.");
|
|
|
|
$this->out();
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->out('Commands:');
|
2009-09-26 21:08:37 +00:00
|
|
|
$this->out();
|
2009-05-30 04:55:44 +00:00
|
|
|
$this->out("model");
|
|
|
|
$this->out("\tbakes model in interactive mode.");
|
2009-09-26 21:08:37 +00:00
|
|
|
$this->out();
|
2009-05-30 04:55:44 +00:00
|
|
|
$this->out("model <name>");
|
|
|
|
$this->out("\tbakes model file with no associations or validation");
|
2009-09-26 21:08:37 +00:00
|
|
|
$this->out();
|
2009-05-30 04:55:44 +00:00
|
|
|
$this->out("model all");
|
|
|
|
$this->out("\tbakes all model files with associations and validation");
|
2009-09-26 21:08:37 +00:00
|
|
|
$this->out();
|
2008-06-04 19:04:58 +00:00
|
|
|
$this->_stop();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-04-30 01:18:08 +00:00
|
|
|
* Interact with FixtureTask to automatically bake fixtures when baking models.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-07-07 02:20:03 +00:00
|
|
|
* @param string $className Name of class to bake fixture for
|
|
|
|
* @param string $useTable Optional table name for fixture to use.
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
* @see FixtureTask::bake
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-05-15 01:42:07 +00:00
|
|
|
function bakeFixture($className, $useTable = null) {
|
2009-05-05 04:10:24 +00:00
|
|
|
$this->Fixture->connection = $this->connection;
|
2009-06-07 00:37:01 +00:00
|
|
|
$this->Fixture->plugin = $this->plugin;
|
2009-04-30 01:18:08 +00:00
|
|
|
$this->Fixture->bake($className, $useTable);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-08-02 20:31:01 +00:00
|
|
|
?>
|