2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* The ModelTask handles creating and updating models files.
|
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2008-05-30 11:40:08 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) 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
|
|
|
* @since CakePHP(tm) v 1.2
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-11-25 21:05:00 +00:00
|
|
|
App::uses('AppShell', 'Console/Command');
|
2010-12-07 06:14:17 +00:00
|
|
|
App::uses('BakeTask', 'Console/Command/Task');
|
|
|
|
App::uses('ConnectionManager', 'Model');
|
2010-12-09 03:45:18 +00:00
|
|
|
App::uses('Model', 'Model');
|
2011-03-08 05:20:07 +00:00
|
|
|
App::uses('Validation', 'Utility');
|
2010-03-05 01:52:25 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Task class for creating and updating model files.
|
|
|
|
*
|
2011-11-19 22:51:39 +00:00
|
|
|
* @package Cake.Console.Command.Task
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-03-05 01:52:25 +00:00
|
|
|
class ModelTask extends BakeTask {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-04-17 10:05:56 +00:00
|
|
|
* path to Model directory
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-04-17 10:05:56 +00:00
|
|
|
public $path = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* tasks
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $tasks = array('DbConfig', 'Fixture', 'Test', 'Template');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-02-13 23:15:11 +00:00
|
|
|
/**
|
|
|
|
* Tables to skip when running all()
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $skipTables = array('i18n');
|
2010-02-13 23:15:11 +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
|
|
|
*/
|
2010-04-04 06:36:12 +00:00
|
|
|
protected $_tables = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-11-25 15:25:11 +00:00
|
|
|
/**
|
|
|
|
* Holds the model names
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_modelNames = array();
|
|
|
|
|
2009-05-08 04:49:26 +00:00
|
|
|
/**
|
|
|
|
* Holds validation method map.
|
|
|
|
*
|
|
|
|
* @var array
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-04-04 06:36:12 +00:00
|
|
|
protected $_validations = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-04-17 10:05:56 +00:00
|
|
|
/**
|
|
|
|
* Override initialize
|
|
|
|
*
|
2011-07-29 02:03:44 +00:00
|
|
|
* @return void
|
2011-04-17 10:05:56 +00:00
|
|
|
*/
|
|
|
|
public function initialize() {
|
2011-04-17 11:28:45 +00:00
|
|
|
$this->path = current(App::path('Model'));
|
2011-04-17 10:05:56 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Execution method always used for tasks
|
|
|
|
*
|
2011-07-29 02:03:44 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function execute() {
|
2010-10-19 03:09:23 +00:00
|
|
|
parent::execute();
|
2009-11-10 04:54:59 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (empty($this->args)) {
|
2010-04-24 01:49:35 +00:00
|
|
|
$this->_interactive();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->args[0])) {
|
2009-05-08 04:49:26 +00:00
|
|
|
$this->interactive = false;
|
|
|
|
if (!isset($this->connection)) {
|
|
|
|
$this->connection = 'default';
|
|
|
|
}
|
2013-02-12 02:38:08 +00:00
|
|
|
if (strtolower($this->args[0]) === 'all') {
|
2009-04-28 03:15:28 +00:00
|
|
|
return $this->all();
|
|
|
|
}
|
2010-03-06 02:30:58 +00:00
|
|
|
$model = $this->_modelName($this->args[0]);
|
2011-12-05 11:21:18 +00:00
|
|
|
$this->listAll($this->connection);
|
|
|
|
$useTable = $this->getTable($model);
|
|
|
|
$object = $this->_getModelObject($model, $useTable);
|
2009-05-08 04:49:26 +00:00
|
|
|
if ($this->bake($object, false)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->_checkUnitTest()) {
|
2011-12-05 11:21:18 +00:00
|
|
|
$this->bakeFixture($model, $useTable);
|
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
|
|
|
*/
|
2010-04-24 01:49:35 +00:00
|
|
|
public 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();
|
2010-02-13 22:55:34 +00:00
|
|
|
foreach ($this->_tables as $table) {
|
2010-02-13 23:15:11 +00:00
|
|
|
if (in_array($table, $this->skipTables)) {
|
|
|
|
continue;
|
|
|
|
}
|
2009-04-28 03:15:28 +00:00
|
|
|
$modelClass = Inflector::classify($table);
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'Baking %s', $modelClass));
|
2011-11-25 15:25:11 +00:00
|
|
|
$object = $this->_getModelObject($modelClass, $table);
|
2009-07-15 14:00:35 +00:00
|
|
|
if ($this->bake($object, false) && $unitTestExists) {
|
2011-11-25 15:25:11 +00:00
|
|
|
$this->bakeFixture($modelClass, $table);
|
2009-07-15 14:00:35 +00:00
|
|
|
$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.
|
2011-07-29 02:03:44 +00:00
|
|
|
* @param string $table Table name
|
|
|
|
* @return Model Model instance
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2011-11-25 15:25:11 +00:00
|
|
|
protected function _getModelObject($className, $table = null) {
|
2009-11-16 14:24:52 +00:00
|
|
|
if (!$table) {
|
|
|
|
$table = Inflector::tableize($className);
|
|
|
|
}
|
2010-11-13 04:05:44 +00:00
|
|
|
$object = new Model(array('name' => $className, 'table' => $table, 'ds' => $this->connection));
|
2011-11-25 15:25:11 +00:00
|
|
|
$fields = $object->schema(true);
|
|
|
|
foreach ($fields as $name => $field) {
|
2013-02-12 02:38:08 +00:00
|
|
|
if (isset($field['key']) && $field['key'] === 'primary') {
|
2011-11-25 15:25:11 +00:00
|
|
|
$object->primaryKey = $name;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
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.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $default The default option for the given prompt.
|
|
|
|
* @return int Result of user choice.
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-04-24 01:49:35 +00:00
|
|
|
public function inOptions($options, $prompt = null, $default = null) {
|
2009-05-10 05:35:59 +00:00
|
|
|
$valid = false;
|
|
|
|
$max = count($options);
|
|
|
|
while (!$valid) {
|
2012-06-11 01:53:19 +00:00
|
|
|
$len = strlen(count($options) + 1);
|
2009-05-10 05:35:59 +00:00
|
|
|
foreach ($options as $i => $option) {
|
2012-06-04 12:57:14 +00:00
|
|
|
$this->out(sprintf("%${len}d. %s", $i + 1, $option));
|
2009-05-10 05:35:59 +00:00
|
|
|
}
|
|
|
|
if (empty($prompt)) {
|
2011-03-19 17:32:35 +00:00
|
|
|
$prompt = __d('cake_console', 'Make a selection from the choices above');
|
2009-05-10 05:35:59 +00:00
|
|
|
}
|
|
|
|
$choice = $this->in($prompt, null, $default);
|
2014-09-10 13:52:57 +00:00
|
|
|
if ((int)$choice > 0 && (int)$choice <= $max) {
|
2009-05-10 05:35:59 +00:00
|
|
|
$valid = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $choice - 1;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Handles interactive baking
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-24 01:49:35 +00:00
|
|
|
protected function _interactive() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->hr();
|
2012-02-05 17:00:30 +00:00
|
|
|
$this->out(__d('cake_console', "Bake Model\nPath: %s", $this->getPath()));
|
2008-05-30 11:40:08 +00:00
|
|
|
$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);
|
2010-11-13 04:05:44 +00:00
|
|
|
$db = ConnectionManager::getDataSource($this->connection);
|
2009-04-29 01:20:34 +00:00
|
|
|
$fullTableName = $db->fullTableName($useTable);
|
2011-11-19 22:51:39 +00:00
|
|
|
if (!in_array($useTable, $this->_tables)) {
|
2011-11-20 00:11:33 +00:00
|
|
|
$prompt = __d('cake_console', "The table %s doesn't exist or could not be automatically detected\ncontinue anyway?", $useTable);
|
2011-11-19 22:51:39 +00:00
|
|
|
$continue = $this->in($prompt, array('y', 'n'));
|
2013-02-12 02:38:08 +00:00
|
|
|
if (strtolower($continue) === 'n') {
|
2011-11-19 22:51:39 +00:00
|
|
|
return false;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-19 22:51:39 +00:00
|
|
|
|
|
|
|
$tempModel = new Model(array('name' => $currentModelName, 'table' => $useTable, 'ds' => $this->connection));
|
2011-11-20 00:11:33 +00:00
|
|
|
|
|
|
|
$knownToExist = false;
|
|
|
|
try {
|
|
|
|
$fields = $tempModel->schema(true);
|
|
|
|
$knownToExist = true;
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$fields = array($tempModel->primaryKey);
|
|
|
|
}
|
2011-11-19 22:51:39 +00:00
|
|
|
if (!array_key_exists('id', $fields)) {
|
|
|
|
$primaryKey = $this->findPrimaryKey($fields);
|
|
|
|
}
|
|
|
|
|
2011-11-20 00:11:33 +00:00
|
|
|
if ($knownToExist) {
|
|
|
|
$displayField = $tempModel->hasField(array('name', 'title'));
|
|
|
|
if (!$displayField) {
|
|
|
|
$displayField = $this->findDisplayField($tempModel->schema());
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-11-20 00:11:33 +00:00
|
|
|
$prompt = __d('cake_console', "Would you like to supply validation criteria \nfor the fields in your model?");
|
2013-06-09 15:39:48 +00:00
|
|
|
$wannaDoValidation = $this->in($prompt, array('y', 'n'), 'y');
|
2013-02-12 02:38:08 +00:00
|
|
|
if (array_search($useTable, $this->_tables) !== false && strtolower($wannaDoValidation) === 'y') {
|
2011-11-20 00:11:33 +00:00
|
|
|
$validate = $this->doValidation($tempModel);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-11-20 00:11:33 +00:00
|
|
|
$prompt = __d('cake_console', "Would you like to define model associations\n(hasMany, hasOne, belongsTo, etc.)?");
|
2013-06-09 15:39:48 +00:00
|
|
|
$wannaDoAssoc = $this->in($prompt, array('y', 'n'), 'y');
|
2013-02-12 02:38:08 +00:00
|
|
|
if (strtolower($wannaDoAssoc) === 'y') {
|
2011-11-20 00:11:33 +00:00
|
|
|
$associations = $this->doAssociations($tempModel);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2009-09-26 21:08:37 +00:00
|
|
|
$this->out();
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->hr();
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'The following Model will be created:'));
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->hr();
|
2011-04-25 19:17:59 +00:00
|
|
|
$this->out(__d('cake_console', "Name: %s", $currentModelName));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-05-05 04:10:24 +00:00
|
|
|
if ($this->connection !== 'default') {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', "DB Config: %s", $this->connection));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if ($fullTableName !== Inflector::tableize($currentModelName)) {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'DB Table: %s', $fullTableName));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2013-02-12 02:38:08 +00:00
|
|
|
if ($primaryKey !== 'id') {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'Primary Key: %s', $primaryKey));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if (!empty($validate)) {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'Validation: %s', print_r($validate, true)));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if (!empty($associations)) {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'Associations:'));
|
2009-05-05 04:43:29 +00:00
|
|
|
$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();
|
2011-12-01 07:21:31 +00:00
|
|
|
$looksGood = $this->in(__d('cake_console', 'Look okay?'), array('y', 'n'), 'y');
|
2009-07-09 03:30:29 +00:00
|
|
|
|
2013-02-12 02:38:08 +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.
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-04-24 01:49:35 +00:00
|
|
|
protected function _printAssociation($modelName, $type, $associations) {
|
2009-05-05 04:43:29 +00:00
|
|
|
if (!empty($associations[$type])) {
|
2012-03-03 23:55:07 +00:00
|
|
|
for ($i = 0, $len = count($associations[$type]); $i < $len; $i++) {
|
2009-05-05 04:43:29 +00:00
|
|
|
$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-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function findPrimaryKey($fields) {
|
2011-12-01 19:30:36 +00:00
|
|
|
$name = 'id';
|
2009-05-05 04:43:29 +00:00
|
|
|
foreach ($fields as $name => $field) {
|
2013-02-12 02:38:08 +00:00
|
|
|
if (isset($field['key']) && $field['key'] === 'primary') {
|
2009-05-05 04:43:29 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-03-19 17:32:35 +00:00
|
|
|
return $this->in(__d('cake_console', 'What is the primaryKey?'), null, $name);
|
2009-05-05 04:43:29 +00:00
|
|
|
}
|
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
|
|
|
*/
|
2010-04-24 01:49:35 +00:00
|
|
|
public function findDisplayField($fields) {
|
2009-07-08 13:10:18 +00:00
|
|
|
$fieldNames = array_keys($fields);
|
2011-03-19 17:32:35 +00:00
|
|
|
$prompt = __d('cake_console', "A displayField could not be automatically detected\nwould you like to choose one?");
|
2009-07-08 13:10:18 +00:00
|
|
|
$continue = $this->in($prompt, array('y', 'n'));
|
2013-02-12 02:38:08 +00:00
|
|
|
if (strtolower($continue) === 'n') {
|
2009-07-08 13:10:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-03-19 17:32:35 +00:00
|
|
|
$prompt = __d('cake_console', 'Choose a field from the options above:');
|
2009-07-08 13:10:18 +00:00
|
|
|
$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
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param Model $model Model to have validations generated for.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return array validate Array of user selected validations.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-03-26 04:04:25 +00:00
|
|
|
public function doValidation($model) {
|
2013-03-15 11:14:06 +00:00
|
|
|
if (!$model instanceof Model) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-13 12:36:19 +00:00
|
|
|
$fields = $model->schema();
|
2008-05-30 11:40:08 +00:00
|
|
|
if (empty($fields)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-13 12:36:19 +00:00
|
|
|
|
|
|
|
$skipFields = 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);
|
2013-11-13 12:36:19 +00:00
|
|
|
if (isset($validation['_skipFields'])) {
|
|
|
|
unset($validation['_skipFields']);
|
|
|
|
$skipFields = true;
|
|
|
|
}
|
2009-05-08 04:49:26 +00:00
|
|
|
if (!empty($validation)) {
|
|
|
|
$validate[$fieldName] = $validation;
|
|
|
|
}
|
2013-11-13 12:36:19 +00:00
|
|
|
if ($skipFields) {
|
|
|
|
return $validate;
|
|
|
|
}
|
2009-05-08 04:49:26 +00:00
|
|
|
}
|
|
|
|
return $validate;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-08 04:49:26 +00:00
|
|
|
/**
|
2010-02-13 22:55:34 +00:00
|
|
|
* Populate the _validations array
|
2009-05-08 04:49:26 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-04-24 01:49:35 +00:00
|
|
|
public function initValidations() {
|
2009-05-08 04:49:26 +00:00
|
|
|
$options = $choices = array();
|
2008-05-30 11:40:08 +00:00
|
|
|
if (class_exists('Validation')) {
|
2009-10-07 04:51:55 +00:00
|
|
|
$options = get_class_methods('Validation');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-05-08 04:49:26 +00:00
|
|
|
sort($options);
|
|
|
|
$default = 1;
|
2012-12-20 12:47:03 +00:00
|
|
|
foreach ($options as $option) {
|
2013-02-12 02:38:08 +00:00
|
|
|
if ($option{0} !== '_') {
|
2013-09-18 16:31:55 +00:00
|
|
|
$choices[$default] = $option;
|
2009-05-08 04:49:26 +00:00
|
|
|
$default++;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-05-08 04:49:26 +00:00
|
|
|
}
|
2011-05-16 13:29:23 +00:00
|
|
|
$choices[$default] = 'none'; // Needed since index starts at 1
|
2010-02-13 22:55:34 +00:00
|
|
|
$this->_validations = $choices;
|
2009-05-08 04:49:26 +00:00
|
|
|
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
|
2014-05-28 03:34:53 +00:00
|
|
|
* @param string $primaryKey The primary key field.
|
2009-05-08 04:49:26 +00:00
|
|
|
* @return array Array of validation for the field.
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-04-24 01:49:35 +00:00
|
|
|
public function fieldValidation($fieldName, $metaData, $primaryKey = 'id') {
|
2010-02-13 22:55:34 +00:00
|
|
|
$defaultChoice = count($this->_validations);
|
2009-05-09 04:35:03 +00:00
|
|
|
$validate = $alreadyChosen = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2013-11-13 12:36:19 +00:00
|
|
|
$prompt = __d('cake_console',
|
|
|
|
"or enter in a valid regex validation string.\nAlternatively [s] skip the rest of the fields.\n"
|
|
|
|
);
|
|
|
|
$methods = array_flip($this->_validations);
|
|
|
|
|
2009-05-08 05:09:19 +00:00
|
|
|
$anotherValidator = 'y';
|
2013-02-12 02:38:08 +00:00
|
|
|
while ($anotherValidator === 'y') {
|
2009-05-08 05:09:19 +00:00
|
|
|
if ($this->interactive) {
|
2009-09-26 21:08:37 +00:00
|
|
|
$this->out();
|
2012-06-04 06:01:22 +00:00
|
|
|
$this->out(__d('cake_console', 'Field: <info>%s</info>', $fieldName));
|
|
|
|
$this->out(__d('cake_console', 'Type: <info>%s</info>', $metaData['type']));
|
2009-05-08 05:09:19 +00:00
|
|
|
$this->hr();
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'Please select one of the following validation options:'));
|
2009-05-08 05:09:19 +00:00
|
|
|
$this->hr();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-06-11 01:53:19 +00:00
|
|
|
$optionText = '';
|
2012-08-04 03:29:34 +00:00
|
|
|
for ($i = 1, $m = $defaultChoice / 2; $i <= $m; $i++) {
|
2012-06-11 01:53:19 +00:00
|
|
|
$line = sprintf("%2d. %s", $i, $this->_validations[$i]);
|
|
|
|
$optionText .= $line . str_repeat(" ", 31 - strlen($line));
|
2012-08-04 03:29:34 +00:00
|
|
|
if ($m + $i !== $defaultChoice) {
|
|
|
|
$optionText .= sprintf("%2d. %s\n", $m + $i, $this->_validations[$m + $i]);
|
|
|
|
}
|
2012-06-11 01:53:19 +00:00
|
|
|
}
|
|
|
|
$this->out($optionText);
|
|
|
|
$this->out(__d('cake_console', "%s - Do not do any validation on this field.", $defaultChoice));
|
|
|
|
$this->hr();
|
2009-05-08 05:09:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$guess = $defaultChoice;
|
|
|
|
if ($metaData['null'] != 1 && !in_array($fieldName, array($primaryKey, 'created', 'modified', 'updated'))) {
|
2013-02-12 02:38:08 +00:00
|
|
|
if ($fieldName === 'email') {
|
2009-05-08 05:09:19 +00:00
|
|
|
$guess = $methods['email'];
|
2013-02-12 02:38:08 +00:00
|
|
|
} elseif ($metaData['type'] === 'string' && $metaData['length'] == 36) {
|
2011-05-16 13:29:23 +00:00
|
|
|
$guess = $methods['uuid'];
|
2013-02-12 02:38:08 +00:00
|
|
|
} elseif ($metaData['type'] === 'string') {
|
2013-09-18 16:31:55 +00:00
|
|
|
$guess = $methods['notEmpty'];
|
2013-02-12 02:38:08 +00:00
|
|
|
} elseif ($metaData['type'] === 'text') {
|
2013-09-18 16:31:55 +00:00
|
|
|
$guess = $methods['notEmpty'];
|
2013-02-12 02:38:08 +00:00
|
|
|
} elseif ($metaData['type'] === 'integer') {
|
2009-05-08 05:09:19 +00:00
|
|
|
$guess = $methods['numeric'];
|
2013-10-21 02:54:30 +00:00
|
|
|
} elseif ($metaData['type'] === 'float') {
|
|
|
|
$guess = $methods['numeric'];
|
2013-02-12 02:38:08 +00:00
|
|
|
} elseif ($metaData['type'] === 'boolean') {
|
2009-07-30 02:20:07 +00:00
|
|
|
$guess = $methods['boolean'];
|
2013-02-12 02:38:08 +00:00
|
|
|
} elseif ($metaData['type'] === 'date') {
|
2009-05-08 05:09:19 +00:00
|
|
|
$guess = $methods['date'];
|
2013-02-12 02:38:08 +00:00
|
|
|
} elseif ($metaData['type'] === 'time') {
|
2009-05-08 05:09:19 +00:00
|
|
|
$guess = $methods['time'];
|
2013-02-12 02:38:08 +00:00
|
|
|
} elseif ($metaData['type'] === 'datetime') {
|
2012-06-08 07:18:52 +00:00
|
|
|
$guess = $methods['datetime'];
|
2013-02-12 02:38:08 +00:00
|
|
|
} elseif ($metaData['type'] === 'inet') {
|
2011-09-20 17:45:35 +00:00
|
|
|
$guess = $methods['ip'];
|
2009-05-08 05:09:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->interactive === true) {
|
|
|
|
$choice = $this->in($prompt, null, $guess);
|
2013-11-13 12:36:19 +00:00
|
|
|
if ($choice === 's') {
|
|
|
|
$validate['_skipFields'] = true;
|
|
|
|
return $validate;
|
|
|
|
}
|
2009-05-09 04:35:03 +00:00
|
|
|
if (in_array($choice, $alreadyChosen)) {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', "You have already chosen that validation rule,\nplease choose again"));
|
2009-05-09 04:35:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
2010-03-14 00:44:20 +00:00
|
|
|
if (!isset($this->_validations[$choice]) && is_numeric($choice)) {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'Please make a valid selection.'));
|
2010-03-13 03:58:14 +00:00
|
|
|
continue;
|
|
|
|
}
|
2009-05-08 05:09:19 +00:00
|
|
|
$alreadyChosen[] = $choice;
|
|
|
|
} else {
|
|
|
|
$choice = $guess;
|
|
|
|
}
|
2010-03-14 00:44:20 +00:00
|
|
|
|
|
|
|
if (isset($this->_validations[$choice])) {
|
|
|
|
$validatorName = $this->_validations[$choice];
|
|
|
|
} else {
|
|
|
|
$validatorName = Inflector::slug($choice);
|
|
|
|
}
|
|
|
|
|
2009-05-08 05:09:19 +00:00
|
|
|
if ($choice != $defaultChoice) {
|
2012-09-14 17:54:29 +00:00
|
|
|
$validate[$validatorName] = $choice;
|
2010-02-13 22:55:34 +00:00
|
|
|
if (is_numeric($choice) && isset($this->_validations[$choice])) {
|
|
|
|
$validate[$validatorName] = $this->_validations[$choice];
|
2009-05-08 05:09:19 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-14 17:54:29 +00:00
|
|
|
$anotherValidator = 'n';
|
|
|
|
if ($this->interactive && $choice != $defaultChoice) {
|
2013-11-13 12:36:19 +00:00
|
|
|
$anotherValidator = $this->in(__d('cake_console', "Would you like to add another validation rule\n" .
|
|
|
|
"or skip the rest of the fields?"), array('y', 'n', 's'), 'n');
|
|
|
|
if ($anotherValidator === 's') {
|
|
|
|
$validate['_skipFields'] = true;
|
|
|
|
return $validate;
|
|
|
|
}
|
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
|
|
|
|
*
|
2014-05-28 03:34:53 +00:00
|
|
|
* @param Model $model The model object
|
2013-03-15 11:14:06 +00:00
|
|
|
* @return array Associations
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-03-26 04:04:25 +00:00
|
|
|
public function doAssociations($model) {
|
2013-03-15 11:14:06 +00:00
|
|
|
if (!$model instanceof Model) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-07-16 12:52:33 +00:00
|
|
|
if ($this->interactive === true) {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'One moment while the associations are detected.'));
|
2009-07-16 12:52:33 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-12-30 03:23:03 +00:00
|
|
|
$fields = $model->schema(true);
|
2008-05-30 11:40:08 +00:00
|
|
|
if (empty($fields)) {
|
2011-11-25 15:25:11 +00:00
|
|
|
return array();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2010-02-21 01:13:42 +00:00
|
|
|
if (empty($this->_tables)) {
|
2012-03-03 23:55:07 +00:00
|
|
|
$this->_tables = (array)$this->getAllTables();
|
2010-02-21 01:13:42 +00:00
|
|
|
}
|
|
|
|
|
2009-05-05 04:43:29 +00:00
|
|
|
$associations = array(
|
2012-03-03 23:55:07 +00:00
|
|
|
'belongsTo' => array(),
|
|
|
|
'hasMany' => array(),
|
|
|
|
'hasOne' => array(),
|
|
|
|
'hasAndBelongsToMany' => array()
|
2009-05-05 04:43:29 +00:00
|
|
|
);
|
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)) {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'None found.'));
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'Please confirm the following associations:'));
|
2008-05-30 11:40:08 +00:00
|
|
|
$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
|
|
|
|
2013-03-15 11:14:06 +00:00
|
|
|
/**
|
|
|
|
* Handles behaviors
|
|
|
|
*
|
2014-05-28 03:34:53 +00:00
|
|
|
* @param Model $model The model object.
|
2013-03-15 11:14:06 +00:00
|
|
|
* @return array Behaviors
|
|
|
|
*/
|
|
|
|
public function doActsAs($model) {
|
|
|
|
if (!$model instanceof Model) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$behaviors = array();
|
|
|
|
$fields = $model->schema(true);
|
|
|
|
if (empty($fields)) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($fields['lft']) && $fields['lft']['type'] === 'integer' &&
|
|
|
|
isset($fields['rght']) && $fields['rght']['type'] === 'integer' &&
|
|
|
|
isset($fields['parent_id'])) {
|
|
|
|
$behaviors[] = 'Tree';
|
|
|
|
}
|
|
|
|
return $behaviors;
|
|
|
|
}
|
|
|
|
|
2009-05-09 06:20:46 +00:00
|
|
|
/**
|
|
|
|
* Find belongsTo relations and add them to the associations list.
|
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param Model $model Model instance of model being generated.
|
2011-12-02 05:58:09 +00:00
|
|
|
* @param array $associations Array of in progress associations
|
2013-03-15 11:14:06 +00:00
|
|
|
* @return array Associations with belongsTo added in.
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2012-02-23 14:06:25 +00:00
|
|
|
public function findBelongsTo(Model $model, $associations) {
|
2012-12-20 12:47:03 +00:00
|
|
|
$fieldNames = array_keys($model->schema(true));
|
|
|
|
foreach ($fieldNames as $fieldName) {
|
2014-04-05 01:26:31 +00:00
|
|
|
$offset = substr($fieldName, -3) === '_id';
|
2013-02-12 02:38:08 +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,
|
|
|
|
);
|
2013-02-12 02:38:08 +00:00
|
|
|
} elseif ($fieldName === 'parent_id') {
|
2009-05-10 03:32:22 +00:00
|
|
|
$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
|
|
|
/**
|
2013-03-05 07:05:14 +00:00
|
|
|
* Find the hasOne and hasMany relations and add them to associations list
|
2009-05-09 06:20:46 +00:00
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param Model $model Model instance being generated
|
2011-12-02 05:58:09 +00:00
|
|
|
* @param array $associations Array of in progress associations
|
2013-03-15 11:14:06 +00:00
|
|
|
* @return array Associations with hasOne and hasMany added in.
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2012-02-23 14:06:25 +00:00
|
|
|
public function findHasOneAndMany(Model $model, $associations) {
|
2009-05-09 06:20:46 +00:00
|
|
|
$foreignKey = $this->_modelKey($model->name);
|
2010-02-13 22:55:34 +00:00
|
|
|
foreach ($this->_tables as $otherTable) {
|
2009-11-16 14:24:52 +00:00
|
|
|
$tempOtherModel = $this->_getModelObject($this->_modelName($otherTable), $otherTable);
|
2012-12-20 12:47:03 +00:00
|
|
|
$tempFieldNames = array_keys($tempOtherModel->schema(true));
|
2009-05-09 06:20:46 +00:00
|
|
|
|
2009-05-10 01:28:51 +00:00
|
|
|
$pattern = '/_' . preg_quote($model->table, '/') . '|' . preg_quote($model->table, '/') . '_/';
|
2011-11-20 00:17:07 +00:00
|
|
|
$possibleJoinTable = preg_match($pattern, $otherTable);
|
2012-09-14 17:54:29 +00:00
|
|
|
if ($possibleJoinTable) {
|
2009-05-10 03:32:22 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-12-20 12:47:03 +00:00
|
|
|
foreach ($tempFieldNames as $fieldName) {
|
2009-05-10 03:32:22 +00:00
|
|
|
$assoc = false;
|
2014-04-29 12:19:33 +00:00
|
|
|
if ($fieldName !== $model->primaryKey && $fieldName === $foreignKey) {
|
2009-05-09 06:20:46 +00:00
|
|
|
$assoc = array(
|
|
|
|
'alias' => $tempOtherModel->name,
|
|
|
|
'className' => $tempOtherModel->name,
|
|
|
|
'foreignKey' => $fieldName
|
|
|
|
);
|
2014-04-29 12:19:33 +00:00
|
|
|
} elseif ($otherTable === $model->table && $fieldName === 'parent_id') {
|
2009-05-10 03:32:22 +00:00
|
|
|
$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
|
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param Model $model Model instance being generated
|
2011-04-25 19:17:59 +00:00
|
|
|
* @param array $associations Array of in-progress associations
|
2013-03-15 11:14:06 +00:00
|
|
|
* @return array Associations with hasAndBelongsToMany added in.
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2012-02-23 14:06:25 +00:00
|
|
|
public function findHasAndBelongsToMany(Model $model, $associations) {
|
2009-05-09 06:20:46 +00:00
|
|
|
$foreignKey = $this->_modelKey($model->name);
|
2010-02-13 22:55:34 +00:00
|
|
|
foreach ($this->_tables as $otherTable) {
|
2013-01-16 02:43:30 +00:00
|
|
|
$tableName = null;
|
2009-05-09 06:20:46 +00:00
|
|
|
$offset = strpos($otherTable, $model->table . '_');
|
|
|
|
$otherOffset = strpos($otherTable, '_' . $model->table);
|
|
|
|
|
2013-02-07 01:31:15 +00:00
|
|
|
if ($offset !== false) {
|
2013-01-16 02:43:30 +00:00
|
|
|
$tableName = substr($otherTable, strlen($model->table . '_'));
|
2013-02-07 01:31:15 +00:00
|
|
|
} elseif ($otherOffset !== false) {
|
2013-01-16 02:43:30 +00:00
|
|
|
$tableName = substr($otherTable, 0, $otherOffset);
|
|
|
|
}
|
|
|
|
if ($tableName && in_array($tableName, $this->_tables)) {
|
|
|
|
$habtmName = $this->_modelName($tableName);
|
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
|
|
|
*/
|
2012-02-23 14:06:25 +00:00
|
|
|
public function confirmAssociations(Model $model, $associations) {
|
2009-05-10 03:32:22 +00:00
|
|
|
foreach ($associations as $type => $settings) {
|
|
|
|
if (!empty($associations[$type])) {
|
2009-12-16 04:14:03 +00:00
|
|
|
foreach ($associations[$type] as $i => $assoc) {
|
|
|
|
$prompt = "{$model->name} {$type} {$assoc['alias']}?";
|
2011-12-01 07:21:31 +00:00
|
|
|
$response = $this->in($prompt, array('y', 'n'), 'y');
|
2009-05-10 03:32:22 +00:00
|
|
|
|
2013-07-03 17:27:17 +00:00
|
|
|
if (strtolower($response) === 'n') {
|
2009-05-10 03:32:22 +00:00
|
|
|
unset($associations[$type][$i]);
|
2013-02-12 02:38:08 +00:00
|
|
|
} elseif ($type === 'hasMany') {
|
2009-05-10 03:32:22 +00:00
|
|
|
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
|
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param Model $model Temporary model instance
|
2009-05-10 05:04:40 +00:00
|
|
|
* @param array $associations Array of associations.
|
|
|
|
* @return array Array of associations.
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2012-02-23 14:06:25 +00:00
|
|
|
public function doMoreAssociations(Model $model, $associations) {
|
2011-03-19 17:32:35 +00:00
|
|
|
$prompt = __d('cake_console', 'Would you like to define some additional model associations?');
|
2011-12-01 07:21:31 +00:00
|
|
|
$wannaDoMoreAssoc = $this->in($prompt, array('y', 'n'), 'n');
|
2009-05-10 05:04:40 +00:00
|
|
|
$possibleKeys = $this->_generatePossibleKeys();
|
2012-09-28 11:49:51 +00:00
|
|
|
while (strtolower($wannaDoMoreAssoc) === 'y') {
|
2009-05-10 05:35:59 +00:00
|
|
|
$assocs = array('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany');
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'What is the association type?'));
|
2014-09-10 14:29:23 +00:00
|
|
|
$assocType = (int)$this->inOptions($assocs, __d('cake_console', 'Enter a number'));
|
2009-05-10 05:35:59 +00:00
|
|
|
|
2011-11-20 00:17:07 +00:00
|
|
|
$this->out(__d('cake_console', "For the following options be very careful to match your setup exactly.\n" .
|
|
|
|
"Any spelling mistakes will cause errors."));
|
2009-05-10 05:04:40 +00:00
|
|
|
$this->hr();
|
|
|
|
|
2011-03-19 17:32:35 +00:00
|
|
|
$alias = $this->in(__d('cake_console', 'What is the alias for this association?'));
|
2012-09-28 11:49:51 +00:00
|
|
|
$className = $this->in(__d('cake_console', 'What className will %s use?', $alias), null, $alias);
|
2009-05-10 05:04:40 +00:00
|
|
|
|
2012-09-28 11:49:51 +00:00
|
|
|
if ($assocType === 0) {
|
2011-11-20 15:08:01 +00:00
|
|
|
if (!empty($possibleKeys[$model->table])) {
|
|
|
|
$showKeys = $possibleKeys[$model->table];
|
|
|
|
} else {
|
|
|
|
$showKeys = null;
|
|
|
|
}
|
2009-05-10 05:04:40 +00:00
|
|
|
$suggestedForeignKey = $this->_modelKey($alias);
|
|
|
|
} else {
|
|
|
|
$otherTable = Inflector::tableize($className);
|
2010-02-13 22:55:34 +00:00
|
|
|
if (in_array($otherTable, $this->_tables)) {
|
2009-05-10 05:35:59 +00:00
|
|
|
if ($assocType < 3) {
|
2011-11-20 14:44:44 +00:00
|
|
|
if (!empty($possibleKeys[$otherTable])) {
|
|
|
|
$showKeys = $possibleKeys[$otherTable];
|
|
|
|
} else {
|
|
|
|
$showKeys = null;
|
|
|
|
}
|
2009-05-10 05:04:40 +00:00
|
|
|
} else {
|
|
|
|
$showKeys = null;
|
|
|
|
}
|
|
|
|
} else {
|
2011-03-19 17:32:35 +00:00
|
|
|
$otherTable = $this->in(__d('cake_console', 'What is the table for this model?'));
|
2009-05-10 05:04:40 +00:00
|
|
|
$showKeys = $possibleKeys[$otherTable];
|
|
|
|
}
|
|
|
|
$suggestedForeignKey = $this->_modelKey($model->name);
|
|
|
|
}
|
|
|
|
if (!empty($showKeys)) {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'A helpful List of possible keys'));
|
|
|
|
$foreignKey = $this->inOptions($showKeys, __d('cake_console', 'What is the foreignKey?'));
|
2014-09-10 13:52:57 +00:00
|
|
|
$foreignKey = $showKeys[(int)$foreignKey];
|
2009-05-10 05:04:40 +00:00
|
|
|
}
|
|
|
|
if (!isset($foreignKey)) {
|
2011-03-19 17:32:35 +00:00
|
|
|
$foreignKey = $this->in(__d('cake_console', 'What is the foreignKey? Specify your own.'), null, $suggestedForeignKey);
|
2009-05-10 05:04:40 +00:00
|
|
|
}
|
2012-09-28 11:49:51 +00:00
|
|
|
if ($assocType === 3) {
|
2011-03-19 17:32:35 +00:00
|
|
|
$associationForeignKey = $this->in(__d('cake_console', 'What is the associationForeignKey?'), null, $this->_modelKey($model->name));
|
|
|
|
$joinTable = $this->in(__d('cake_console', 'What is the joinTable?'));
|
2009-05-10 05:04:40 +00:00
|
|
|
}
|
|
|
|
$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;
|
2012-09-28 11:49:51 +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;
|
|
|
|
}
|
2011-12-01 07:21:31 +00:00
|
|
|
$wannaDoMoreAssoc = $this->in(__d('cake_console', 'Define another association?'), array('y', 'n'), 'y');
|
2009-05-10 05:04:40 +00:00
|
|
|
}
|
|
|
|
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.
|
|
|
|
*
|
2013-03-15 11:14:06 +00:00
|
|
|
* @return array Array of tables and possible keys
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-04-24 01:49:35 +00:00
|
|
|
protected function _generatePossibleKeys() {
|
2009-05-10 05:04:40 +00:00
|
|
|
$possible = array();
|
2010-02-13 22:55:34 +00:00
|
|
|
foreach ($this->_tables as $otherTable) {
|
2010-12-13 01:55:33 +00:00
|
|
|
$tempOtherModel = new Model(array('table' => $otherTable, 'ds' => $this->connection));
|
2009-12-30 03:23:03 +00:00
|
|
|
$modelFieldsTemp = $tempOtherModel->schema(true);
|
2009-05-10 05:04:40 +00:00
|
|
|
foreach ($modelFieldsTemp as $fieldName => $field) {
|
2013-02-12 02:38:08 +00:00
|
|
|
if ($field['type'] === 'integer' || $field['type'] === 'string') {
|
2009-05-10 05:04:40 +00:00
|
|
|
$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.
|
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|object $name Model name or object
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param array|bool $data if array and $name is not an object assume bake data, otherwise boolean.
|
2011-07-29 02:03:44 +00:00
|
|
|
* @return string
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-24 01:49:35 +00:00
|
|
|
public function bake($name, $data = array()) {
|
2013-03-15 11:14:06 +00:00
|
|
|
if ($name instanceof Model) {
|
2012-09-14 17:26:30 +00:00
|
|
|
if (!$data) {
|
2011-11-25 15:25:11 +00:00
|
|
|
$data = array();
|
|
|
|
$data['associations'] = $this->doAssociations($name);
|
2009-07-09 02:06:42 +00:00
|
|
|
$data['validate'] = $this->doValidation($name);
|
2013-03-15 11:14:06 +00:00
|
|
|
$data['actsAs'] = $this->doActsAs($name);
|
2009-07-09 02:06:42 +00:00
|
|
|
}
|
|
|
|
$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
|
|
|
}
|
2013-03-15 11:14:06 +00:00
|
|
|
|
2012-03-03 23:55:07 +00:00
|
|
|
$defaults = array(
|
|
|
|
'associations' => array(),
|
2013-03-15 11:14:06 +00:00
|
|
|
'actsAs' => array(),
|
2012-03-03 23:55:07 +00:00
|
|
|
'validate' => array(),
|
|
|
|
'primaryKey' => 'id',
|
|
|
|
'useTable' => null,
|
|
|
|
'useDbConfig' => 'default',
|
|
|
|
'displayField' => null
|
|
|
|
);
|
2009-07-09 02:06:42 +00:00
|
|
|
$data = array_merge($defaults, $data);
|
2009-05-14 03:37:21 +00:00
|
|
|
|
2011-11-20 00:17:07 +00:00
|
|
|
$pluginPath = '';
|
|
|
|
if ($this->plugin) {
|
2012-03-03 23:55:07 +00:00
|
|
|
$pluginPath = $this->plugin . '.';
|
2011-11-20 00:17:07 +00:00
|
|
|
}
|
|
|
|
|
2009-07-09 02:06:42 +00:00
|
|
|
$this->Template->set($data);
|
2011-09-10 00:32:01 +00:00
|
|
|
$this->Template->set(array(
|
|
|
|
'plugin' => $this->plugin,
|
2011-11-20 00:17:07 +00:00
|
|
|
'pluginPath' => $pluginPath
|
2011-09-10 00:32:01 +00:00
|
|
|
));
|
2009-07-01 04:50:38 +00:00
|
|
|
$out = $this->Template->generate('classes', 'model');
|
2009-06-07 00:37:01 +00:00
|
|
|
|
2010-03-05 01:52:25 +00:00
|
|
|
$path = $this->getPath();
|
2011-03-16 02:14:10 +00:00
|
|
|
$filename = $path . $name . '.php';
|
2011-04-25 19:17:59 +00:00
|
|
|
$this->out("\n" . __d('cake_console', 'Baking model class for %s...', $name), 1, Shell::QUIET);
|
2009-05-14 03:36:13 +00:00
|
|
|
$this->createFile($filename, $out);
|
2010-02-11 18:59:49 +00:00
|
|
|
ClassRegistry::flush();
|
2009-05-14 03:36:13 +00:00
|
|
|
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
|
2011-07-29 02:03:44 +00:00
|
|
|
* @return string
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-24 01:49:35 +00:00
|
|
|
public function bakeTest($className) {
|
2010-03-05 03:02:49 +00:00
|
|
|
$this->Test->interactive = $this->interactive;
|
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
|
2011-07-29 02:03:44 +00:00
|
|
|
* @return array
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function listAll($useDbConfig = null) {
|
2013-11-23 17:36:15 +00:00
|
|
|
$this->_tables = $this->getAllTables($useDbConfig);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-12-05 11:21:18 +00:00
|
|
|
$this->_modelNames = array();
|
|
|
|
$count = count($this->_tables);
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
|
|
$this->_modelNames[] = $this->_modelName($this->_tables[$i]);
|
|
|
|
}
|
2009-05-15 03:00:29 +00:00
|
|
|
if ($this->interactive === true) {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'Possible Models based on your current database:'));
|
2012-06-11 01:53:19 +00:00
|
|
|
$len = strlen($count + 1);
|
2008-05-30 11:40:08 +00:00
|
|
|
for ($i = 0; $i < $count; $i++) {
|
2012-06-04 12:57:14 +00:00
|
|
|
$this->out(sprintf("%${len}d. %s", $i + 1, $this->_modelNames[$i]));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-13 22:55:34 +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.
|
2010-04-24 01:49:35 +00:00
|
|
|
* @return string Table name
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-04-24 01:49:35 +00:00
|
|
|
public function getTable($modelName, $useDbConfig = null) {
|
2009-04-29 01:20:34 +00:00
|
|
|
$useTable = Inflector::tableize($modelName);
|
2011-11-25 15:25:11 +00:00
|
|
|
if (in_array($modelName, $this->_modelNames)) {
|
|
|
|
$modelNames = array_flip($this->_modelNames);
|
|
|
|
$useTable = $this->_tables[$modelNames[$modelName]];
|
|
|
|
}
|
2009-04-29 01:20:34 +00:00
|
|
|
|
2011-12-05 11:21:18 +00:00
|
|
|
if ($this->interactive === true) {
|
|
|
|
if (!isset($useDbConfig)) {
|
|
|
|
$useDbConfig = $this->connection;
|
|
|
|
}
|
|
|
|
$db = ConnectionManager::getDataSource($useDbConfig);
|
|
|
|
$fullTableName = $db->fullTableName($useTable, false);
|
|
|
|
$tableIsGood = false;
|
|
|
|
if (array_search($useTable, $this->_tables) === false) {
|
|
|
|
$this->out();
|
|
|
|
$this->out(__d('cake_console', "Given your model named '%s',\nCake would expect a database table named '%s'", $modelName, $fullTableName));
|
|
|
|
$tableIsGood = $this->in(__d('cake_console', 'Do you want to use this table?'), array('y', 'n'), 'y');
|
|
|
|
}
|
2013-02-12 02:38:08 +00:00
|
|
|
if (strtolower($tableIsGood) === 'n') {
|
2014-12-23 16:18:32 +00:00
|
|
|
$useTable = $this->in(__d('cake_console', 'What is the name of the table (without prefix)?'));
|
2011-12-05 11:21:18 +00:00
|
|
|
}
|
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
|
|
|
*/
|
2010-04-24 01:49:35 +00:00
|
|
|
public function getAllTables($useDbConfig = null) {
|
2009-05-15 03:00:29 +00:00
|
|
|
if (!isset($useDbConfig)) {
|
|
|
|
$useDbConfig = $this->connection;
|
|
|
|
}
|
2009-11-04 18:21:05 +00:00
|
|
|
|
2009-05-15 03:00:29 +00:00
|
|
|
$tables = array();
|
2010-11-13 04:05:44 +00:00
|
|
|
$db = ConnectionManager::getDataSource($useDbConfig);
|
2009-12-30 03:23:03 +00:00
|
|
|
$db->cacheSources = false;
|
2009-05-15 03:00:29 +00:00
|
|
|
$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)) {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->err(__d('cake_console', 'Your database does not have any tables.'));
|
2013-07-01 12:15:54 +00:00
|
|
|
return $this->_stop();
|
2009-05-15 03:00:29 +00:00
|
|
|
}
|
2013-11-23 17:34:16 +00:00
|
|
|
sort($tables);
|
2009-05-15 03:00:29 +00:00
|
|
|
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.
|
|
|
|
*
|
2011-07-29 02:03:44 +00:00
|
|
|
* @param string $useDbConfig Database config name
|
2013-03-15 11:14:06 +00:00
|
|
|
* @return string The model name
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function getName($useDbConfig = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->listAll($useDbConfig);
|
|
|
|
|
|
|
|
$enteredModel = '';
|
|
|
|
|
2012-09-14 17:42:25 +00:00
|
|
|
while (!$enteredModel) {
|
2011-11-20 00:17:07 +00:00
|
|
|
$enteredModel = $this->in(__d('cake_console', "Enter a number from the list above,\n" .
|
|
|
|
"type in the name of another model, or 'q' to exit"), null, 'q');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($enteredModel === 'q') {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'Exit'));
|
2013-07-01 12:15:54 +00:00
|
|
|
return $this->_stop();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2014-09-10 13:52:57 +00:00
|
|
|
if (!$enteredModel || (int)$enteredModel > count($this->_modelNames)) {
|
2012-03-03 23:55:07 +00:00
|
|
|
$this->err(__d('cake_console', "The model name you supplied was empty,\n" .
|
2011-11-20 00:17:07 +00:00
|
|
|
"or the number you selected was not an option. Please try again."));
|
2008-05-30 11:40:08 +00:00
|
|
|
$enteredModel = '';
|
|
|
|
}
|
|
|
|
}
|
2014-09-10 13:52:57 +00:00
|
|
|
if ((int)$enteredModel > 0 && (int)$enteredModel <= count($this->_modelNames)) {
|
|
|
|
return $this->_modelNames[(int)$enteredModel - 1];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-09-14 17:42:25 +00:00
|
|
|
|
|
|
|
return $enteredModel;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-12-05 23:38:32 +00:00
|
|
|
* Gets the option parser instance and configures it.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2013-12-05 23:38:32 +00:00
|
|
|
* @return ConsoleOptionParser
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-10-13 03:06:07 +00:00
|
|
|
public function getOptionParser() {
|
|
|
|
$parser = parent::getOptionParser();
|
2013-12-05 23:38:32 +00:00
|
|
|
|
|
|
|
$parser->description(
|
|
|
|
__d('cake_console', 'Bake models.')
|
|
|
|
)->addArgument('name', array(
|
|
|
|
'help' => __d('cake_console', 'Name of the model to bake. Can use Plugin.name to bake plugin models.')
|
|
|
|
))->addSubcommand('all', array(
|
|
|
|
'help' => __d('cake_console', 'Bake all model files with associations and validation.')
|
|
|
|
))->addOption('plugin', array(
|
|
|
|
'short' => 'p',
|
|
|
|
'help' => __d('cake_console', 'Plugin to bake the model into.')
|
|
|
|
))->addOption('theme', array(
|
|
|
|
'short' => 't',
|
|
|
|
'help' => __d('cake_console', 'Theme to use when baking code.')
|
|
|
|
))->addOption('connection', array(
|
|
|
|
'short' => 'c',
|
|
|
|
'help' => __d('cake_console', 'The connection the model table is on.')
|
|
|
|
))->addOption('force', array(
|
|
|
|
'short' => 'f',
|
|
|
|
'help' => __d('cake_console', 'Force overwriting existing files without prompting.')
|
|
|
|
))->epilog(
|
|
|
|
__d('cake_console', 'Omitting all arguments and options will enter into an interactive mode.')
|
|
|
|
);
|
|
|
|
|
|
|
|
return $parser;
|
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.
|
|
|
|
* @return void
|
|
|
|
* @see FixtureTask::bake
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-04-24 01:49:35 +00:00
|
|
|
public function bakeFixture($className, $useTable = null) {
|
2010-03-05 03:02:49 +00:00
|
|
|
$this->Fixture->interactive = $this->interactive;
|
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
|
|
|
}
|
2012-03-03 23:55:07 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|