2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* The ModelTask handles creating and updating models files.
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, 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.
|
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, 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
|
|
|
|
2010-03-05 01:52:25 +00:00
|
|
|
include_once dirname(__FILE__) . DS . 'bake.php';
|
|
|
|
|
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
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* path to MODELS directory
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $path = MODELS;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* tasks
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
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
|
|
|
|
* @access protected
|
|
|
|
*/
|
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
|
2010-02-13 22:55:34 +00:00
|
|
|
* @access protected
|
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
|
|
|
|
2009-05-08 04:49:26 +00:00
|
|
|
/**
|
|
|
|
* Holds validation method map.
|
|
|
|
*
|
|
|
|
* @var array
|
2010-02-13 22:55:34 +00:00
|
|
|
* @access protected
|
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
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Execution method always used for tasks
|
|
|
|
*
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
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)) {
|
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';
|
|
|
|
}
|
2009-04-28 03:15:28 +00:00
|
|
|
if (strtolower($this->args[0]) == 'all') {
|
|
|
|
return $this->all();
|
|
|
|
}
|
2010-03-06 02:30:58 +00:00
|
|
|
$model = $this->_modelName($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
|
|
|
*/
|
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);
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(sprintf(__('Baking %s'), $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
|
|
|
*/
|
2010-04-24 01:49:35 +00:00
|
|
|
protected function &_getModelObject($className, $table = null) {
|
2009-11-16 14:24:52 +00:00
|
|
|
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
|
|
|
*/
|
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) {
|
|
|
|
foreach ($options as $i => $option) {
|
|
|
|
$this->out($i + 1 .'. ' . $option);
|
|
|
|
}
|
|
|
|
if (empty($prompt)) {
|
2010-04-15 15:43:39 +00:00
|
|
|
$prompt = __('Make a selection from the choices above');
|
2009-05-10 05:35:59 +00:00
|
|
|
}
|
|
|
|
$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
|
|
|
|
*/
|
2010-04-24 01:49:35 +00:00
|
|
|
protected function _interactive() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$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
|
|
|
|
2010-02-13 22:55:34 +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));
|
2009-12-30 03:23:03 +00:00
|
|
|
$fields = $tempModel->schema(true);
|
2008-05-30 11:40:08 +00:00
|
|
|
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 {
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->err(sprintf(__('Table %s does not exist, cannot bake a model without a table.'), $useTable));
|
2009-07-09 03:30:29 +00:00
|
|
|
$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
|
|
|
|
2010-04-15 15:43:39 +00:00
|
|
|
$prompt = __("Would you like to supply validation criteria \nfor the fields in your model?");
|
2009-05-05 04:43:29 +00:00
|
|
|
$wannaDoValidation = $this->in($prompt, array('y','n'), 'y');
|
2010-02-13 22:55:34 +00:00
|
|
|
if (array_search($useTable, $this->_tables) !== false && strtolower($wannaDoValidation) == 'y') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$validate = $this->doValidation($tempModel);
|
|
|
|
}
|
|
|
|
|
2010-04-15 15:43:39 +00:00
|
|
|
$prompt = __("Would you like to define model associations\n(hasMany, hasOne, belongsTo, etc.)?");
|
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();
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(__('The following Model will be created:'));
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->hr();
|
|
|
|
$this->out("Name: " . $currentModelName);
|
|
|
|
|
2009-05-05 04:10:24 +00:00
|
|
|
if ($this->connection !== 'default') {
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(sprintf(__("DB Config: %s"), $this->connection));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if ($fullTableName !== Inflector::tableize($currentModelName)) {
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(sprintf(__('DB Table: %s'), $fullTableName));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if ($primaryKey != 'id') {
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(sprintf(__('Primary Key: %s'), $primaryKey));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if (!empty($validate)) {
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(sprintf(__('Validation: %s'), print_r($validate, true)));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if (!empty($associations)) {
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(__('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();
|
2010-04-15 15:43:39 +00:00
|
|
|
$looksGood = $this->in(__('Look okay?'), 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.
|
|
|
|
* @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])) {
|
|
|
|
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-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function findPrimaryKey($fields) {
|
2009-05-05 04:43:29 +00:00
|
|
|
foreach ($fields as $name => $field) {
|
|
|
|
if (isset($field['key']) && $field['key'] == 'primary') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-04-15 15:43:39 +00:00
|
|
|
return $this->in(__('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);
|
2010-04-15 15:43:39 +00:00
|
|
|
$prompt = __("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'));
|
|
|
|
if (strtolower($continue) == 'n') {
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-15 15:43:39 +00:00
|
|
|
$prompt = __('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
|
|
|
*
|
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
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public 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
|
|
|
/**
|
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;
|
|
|
|
foreach ($options as $key => $option) {
|
2010-04-24 01:49:35 +00:00
|
|
|
if ($option{0} != '_') {
|
2009-05-08 04:49:26 +00:00
|
|
|
$choices[$default] = strtolower($option);
|
|
|
|
$default++;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-05-08 04:49:26 +00:00
|
|
|
}
|
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
|
|
|
|
* @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
|
|
|
|
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();
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(sprintf(__('Field: %s'), $fieldName));
|
|
|
|
$this->out(sprintf(__('Type: %s'), $metaData['type']));
|
2009-05-08 05:09:19 +00:00
|
|
|
$this->hr();
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(__('Please select one of the following validation options:'));
|
2009-05-08 05:09:19 +00:00
|
|
|
$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++) {
|
2010-02-13 22:55:34 +00:00
|
|
|
$prompt .= $i . ' - ' . $this->_validations[$i] . "\n";
|
2009-05-08 05:09:19 +00:00
|
|
|
}
|
2010-04-15 15:43:39 +00:00
|
|
|
$prompt .= sprintf(__("%s - Do not do any validation on this field.\n"), $defaultChoice);
|
|
|
|
$prompt .= __("... or enter in a valid regex validation string.\n");
|
2009-05-08 05:09:19 +00:00
|
|
|
|
2010-02-13 22:55:34 +00:00
|
|
|
$methods = array_flip($this->_validations);
|
2009-05-08 05:09:19 +00:00
|
|
|
$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)) {
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(__("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)) {
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(__('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) {
|
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
|
|
|
} else {
|
|
|
|
$validate[$validatorName] = $choice;
|
|
|
|
}
|
|
|
|
}
|
2009-05-09 04:35:03 +00:00
|
|
|
if ($this->interactive == true && $choice != $defaultChoice) {
|
2010-04-15 15:43:39 +00:00
|
|
|
$anotherValidator = $this->in(__('Would you like to add another validation rule?'), 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
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public 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) {
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(__('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)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-02-21 01:13:42 +00:00
|
|
|
if (empty($this->_tables)) {
|
|
|
|
$this->_tables = $this->getAllTables();
|
|
|
|
}
|
|
|
|
|
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)) {
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(__('None found.'));
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(__('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
|
|
|
|
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
|
|
|
*/
|
2010-04-24 01:49:35 +00:00
|
|
|
public function findBelongsTo(&$model, $associations) {
|
2009-12-30 03:23:03 +00:00
|
|
|
$fields = $model->schema(true);
|
2009-05-09 06:20:46 +00:00
|
|
|
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
|
|
|
*/
|
2010-04-24 01:49:35 +00:00
|
|
|
public function findHasOneAndMany(&$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);
|
2010-02-21 01:13:42 +00:00
|
|
|
$modelFieldsTemp = $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, '/') . '_/';
|
|
|
|
$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
|
|
|
*/
|
2010-04-24 01:49:35 +00:00
|
|
|
public function findHasAndBelongsToMany(&$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);
|
2009-12-30 03:23:03 +00:00
|
|
|
$modelFieldsTemp = $tempOtherModel->schema(true);
|
2009-05-09 06:20:46 +00:00
|
|
|
|
|
|
|
$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
|
|
|
*/
|
2010-04-24 01:49:35 +00:00
|
|
|
public function confirmAssociations(&$model, $associations) {
|
2009-05-10 03:32:22 +00:00
|
|
|
foreach ($associations as $type => $settings) {
|
|
|
|
if (!empty($associations[$type])) {
|
|
|
|
$count = count($associations[$type]);
|
|
|
|
$response = 'y';
|
2009-12-16 04:14:03 +00:00
|
|
|
foreach ($associations[$type] as $i => $assoc) {
|
|
|
|
$prompt = "{$model->name} {$type} {$assoc['alias']}?";
|
|
|
|
$response = $this->in($prompt, array('y','n'), 'y');
|
2009-05-10 03:32:22 +00:00
|
|
|
|
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
|
|
|
*/
|
2010-04-24 01:49:35 +00:00
|
|
|
public function doMoreAssociations($model, $associations) {
|
2010-04-15 15:43:39 +00:00
|
|
|
$prompt = __('Would you like to define some additional model associations?');
|
2009-05-10 05:04:40 +00:00
|
|
|
$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');
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(__('What is the association type?'));
|
|
|
|
$assocType = intval($this->inOptions($assocs, __('Enter a number')));
|
2009-05-10 05:35:59 +00:00
|
|
|
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(__("For the following options be very careful to match your setup exactly.\nAny spelling mistakes will cause errors."));
|
2009-05-10 05:04:40 +00:00
|
|
|
$this->hr();
|
|
|
|
|
2010-04-15 15:43:39 +00:00
|
|
|
$alias = $this->in(__('What is the alias for this association?'));
|
|
|
|
$className = $this->in(sprintf(__('What className will %s use?'), $alias), null, $alias );
|
2009-05-10 05:04:40 +00:00
|
|
|
$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);
|
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) {
|
2009-05-10 05:04:40 +00:00
|
|
|
$showKeys = $possibleKeys[$otherTable];
|
|
|
|
} else {
|
|
|
|
$showKeys = null;
|
|
|
|
}
|
|
|
|
} else {
|
2010-04-15 15:43:39 +00:00
|
|
|
$otherTable = $this->in(__('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)) {
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(__('A helpful List of possible keys'));
|
|
|
|
$foreignKey = $this->inOptions($showKeys, __('What is the foreignKey?'));
|
2009-05-10 05:35:59 +00:00
|
|
|
$foreignKey = $showKeys[intval($foreignKey)];
|
2009-05-10 05:04:40 +00:00
|
|
|
}
|
|
|
|
if (!isset($foreignKey)) {
|
2010-04-15 15:43:39 +00:00
|
|
|
$foreignKey = $this->in(__('What is the foreignKey? Specify your own.'), null, $suggestedForeignKey);
|
2009-05-10 05:04:40 +00:00
|
|
|
}
|
2009-05-10 05:35:59 +00:00
|
|
|
if ($assocType == 3) {
|
2010-04-15 15:43:39 +00:00
|
|
|
$associationForeignKey = $this->in(__('What is the associationForeignKey?'), null, $this->_modelKey($model->name));
|
|
|
|
$joinTable = $this->in(__('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;
|
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;
|
|
|
|
}
|
2010-04-15 15:43:39 +00:00
|
|
|
$wannaDoMoreAssoc = $this->in(__('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.
|
|
|
|
*
|
|
|
|
* @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) {
|
2009-05-10 05:04:40 +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) {
|
|
|
|
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
|
|
|
*/
|
2010-04-24 01:49:35 +00:00
|
|
|
public 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
|
|
|
|
2010-03-05 01:52:25 +00:00
|
|
|
$path = $this->getPath();
|
2009-06-07 00:37:01 +00:00
|
|
|
$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);
|
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
|
|
|
|
*/
|
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
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function listAll($useDbConfig = null) {
|
2010-02-13 22:55:34 +00:00
|
|
|
$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) {
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(__('Possible Models based on your current database:'));
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->_modelNames = array();
|
2010-02-13 22:55:34 +00:00
|
|
|
$count = count($this->_tables);
|
2008-05-30 11:40:08 +00:00
|
|
|
for ($i = 0; $i < $count; $i++) {
|
2010-02-13 22:55:34 +00:00
|
|
|
$this->_modelNames[] = $this->_modelName($this->_tables[$i]);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->out($i + 1 . ". " . $this->_modelNames[$i]);
|
|
|
|
}
|
|
|
|
}
|
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-05-05 04:10:24 +00:00
|
|
|
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;
|
|
|
|
|
2010-02-13 22:55:34 +00:00
|
|
|
if (array_search($useTable, $this->_tables) === false) {
|
2009-09-26 21:08:37 +00:00
|
|
|
$this->out();
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(sprintf(__("Given your model named '%s',\nCake would expect a database table named '%s'"), $modelName, $fullTableName));
|
|
|
|
$tableIsGood = $this->in(__('Do you want to use this table?'), array('y','n'), 'y');
|
2009-04-29 01:20:34 +00:00
|
|
|
}
|
2009-05-10 16:43:38 +00:00
|
|
|
if (strtolower($tableIsGood) == 'n') {
|
2010-04-15 15:43:39 +00:00
|
|
|
$useTable = $this->in(__('What is the name of the table?'));
|
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
|
|
|
App::import('Model', 'ConnectionManager', false);
|
|
|
|
|
2009-05-15 03:00:29 +00:00
|
|
|
$tables = array();
|
|
|
|
$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)) {
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->err(__('Your database does not have any tables.'));
|
2009-05-15 03:00:29 +00:00
|
|
|
$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
|
|
|
|
*/
|
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 = '';
|
|
|
|
|
|
|
|
while ($enteredModel == '') {
|
2010-04-15 15:43:39 +00:00
|
|
|
$enteredModel = $this->in(__("Enter a number from the list above,\ntype in the name of another model, or 'q' to exit"), null, 'q');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($enteredModel === 'q') {
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(__('Exit'));
|
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)) {
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->err(__("The model name you supplied was empty,\nor the number you selected was not an option. Please try again."));
|
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
|
|
|
|
*
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function help() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$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.
|
|
|
|
* @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
|
|
|
}
|
|
|
|
}
|