2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
/**
|
|
|
|
* The ModelTask handles creating and updating models files.
|
|
|
|
*
|
|
|
|
* Long description for file
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
|
|
|
|
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
2008-10-30 17:30:26 +00:00
|
|
|
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.console.libs.tasks
|
|
|
|
* @since CakePHP(tm) v 1.2
|
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2008-06-20 00:42:08 +00:00
|
|
|
App::import('Model', 'ConnectionManager');
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Task class for creating and updating model files.
|
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.console.libs.tasks
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class ModelTask extends Shell {
|
|
|
|
/**
|
|
|
|
* Name of plugin
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $plugin = null;
|
2009-05-08 04:49:26 +00:00
|
|
|
|
2009-05-05 04:10:24 +00:00
|
|
|
/**
|
|
|
|
* Name of the db connection used.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $connection = null;
|
2009-05-08 04:49:26 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* path to MODELS directory
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $path = MODELS;
|
2009-05-08 04:49:26 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* tasks
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
2009-05-05 04:43:29 +00:00
|
|
|
var $tasks = array('DbConfig', 'Fixture', 'Test');
|
2009-05-08 04:49:26 +00:00
|
|
|
|
2009-04-30 02:25:42 +00:00
|
|
|
/**
|
|
|
|
* Holds tables found on connection.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
**/
|
|
|
|
var $__tables = array();
|
2009-05-08 04:49:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Holds validation method map.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
**/
|
|
|
|
var $__validations = array();
|
|
|
|
|
2009-05-10 03:32:22 +00:00
|
|
|
/**
|
|
|
|
* startup method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function startup() {
|
|
|
|
App::import('Core', 'Model');
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Execution method always used for tasks
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function execute() {
|
|
|
|
if (empty($this->args)) {
|
|
|
|
$this->__interactive();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->args[0])) {
|
2009-05-08 04:49:26 +00:00
|
|
|
$this->interactive = false;
|
|
|
|
if (!isset($this->connection)) {
|
|
|
|
$this->connection = 'default';
|
|
|
|
}
|
2009-04-28 03:15:28 +00:00
|
|
|
if (strtolower($this->args[0]) == 'all') {
|
|
|
|
return $this->all();
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
$model = Inflector::camelize($this->args[0]);
|
2009-05-08 04:49:26 +00:00
|
|
|
$object = $this->_getModelObject($model);
|
|
|
|
if ($this->bake($object, false)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->_checkUnitTest()) {
|
|
|
|
$this->bakeTest($model);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-08 04:49:26 +00:00
|
|
|
|
2009-04-28 03:15:28 +00:00
|
|
|
/**
|
|
|
|
* Bake all models at once.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function all() {
|
|
|
|
$this->listAll($ds, false);
|
2009-04-28 03:58:36 +00:00
|
|
|
|
2009-04-28 03:15:28 +00:00
|
|
|
foreach ($this->__tables as $table) {
|
|
|
|
$modelClass = Inflector::classify($table);
|
|
|
|
$this->out(sprintf(__('Baking %s', true), $modelClass));
|
2009-05-08 04:49:26 +00:00
|
|
|
$this->_getModelObject($modelClass);
|
2009-04-28 03:15:28 +00:00
|
|
|
$this->bake($object, false);
|
|
|
|
}
|
|
|
|
}
|
2009-05-10 05:04:40 +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-05-10 03:32:22 +00:00
|
|
|
function &_getModelObject($className) {
|
|
|
|
$object = new Model(array('name' => $className, 'ds' => $this->connection));
|
2009-05-08 04:49:26 +00:00
|
|
|
return $object;
|
|
|
|
}
|
2009-05-10 05:04:40 +00:00
|
|
|
|
2009-05-10 05:35:59 +00:00
|
|
|
/**
|
|
|
|
* Generate a key value list of options and a prompt.
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
**/
|
|
|
|
function inOptions($options, $prompt = null, $default = null) {
|
|
|
|
$valid = false;
|
|
|
|
$max = count($options);
|
|
|
|
while (!$valid) {
|
|
|
|
foreach ($options as $i => $option) {
|
|
|
|
$this->out($i + 1 .'. ' . $option);
|
|
|
|
}
|
|
|
|
if (empty($prompt)) {
|
|
|
|
$prompt = __('Make a selection from the choices above', true);
|
|
|
|
}
|
|
|
|
$choice = $this->in($prompt, null, $default);
|
|
|
|
if (intval($choice) > 0 && intval($choice) <= $max) {
|
|
|
|
$valid = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $choice - 1;
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Handles interactive baking
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function __interactive() {
|
|
|
|
$this->hr();
|
|
|
|
$this->out(sprintf("Bake Model\nPath: %s", $this->path));
|
|
|
|
$this->hr();
|
|
|
|
$this->interactive = true;
|
|
|
|
|
|
|
|
$primaryKey = 'id';
|
2009-05-05 04:10:24 +00:00
|
|
|
$validate = $associations = array();
|
|
|
|
|
|
|
|
if (empty($this->connection)) {
|
|
|
|
$this->connection = $this->DbConfig->getConfig();
|
|
|
|
}
|
|
|
|
$currentModelName = $this->getName();
|
|
|
|
$useTable = $this->getTable($currentModelName);
|
|
|
|
$db =& ConnectionManager::getDataSource($this->connection);
|
2009-04-29 01:20:34 +00:00
|
|
|
$fullTableName = $db->fullTableName($useTable);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (in_array($useTable, $this->__tables)) {
|
2009-05-05 04:10:24 +00:00
|
|
|
$tempModel = new Model(array('name' => $currentModelName, 'table' => $useTable, 'ds' => $this->connection));
|
2008-05-30 11:40:08 +00:00
|
|
|
$fields = $tempModel->schema();
|
|
|
|
if (!array_key_exists('id', $fields)) {
|
2009-05-05 04:43:29 +00:00
|
|
|
$primaryKey = $this->findPrimaryKey($fields);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-09 05:00:14 +00:00
|
|
|
$prompt = __("Would you like to supply validation criteria \nfor the fields in your model?", true);
|
2009-05-05 04:43:29 +00:00
|
|
|
$wannaDoValidation = $this->in($prompt, array('y','n'), 'y');
|
|
|
|
if (array_search($useTable, $this->__tables) !== false && strtolower($wannaDoValidation) == 'y') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$validate = $this->doValidation($tempModel);
|
|
|
|
}
|
|
|
|
|
2009-05-09 05:00:14 +00:00
|
|
|
$prompt = __("Would you like to define model associations\n(hasMany, hasOne, belongsTo, etc.)?", true);
|
2009-05-05 04:43:29 +00:00
|
|
|
$wannaDoAssoc = $this->in($prompt, array('y','n'), 'y');
|
|
|
|
if (strtolower($wannaDoAssoc) == 'y') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$associations = $this->doAssociations($tempModel);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->out('');
|
|
|
|
$this->hr();
|
|
|
|
$this->out(__('The following Model will be created:', true));
|
|
|
|
$this->hr();
|
|
|
|
$this->out("Name: " . $currentModelName);
|
|
|
|
|
2009-05-05 04:10:24 +00:00
|
|
|
if ($this->connection !== 'default') {
|
2009-05-05 04:43:29 +00:00
|
|
|
$this->out(sprintf(__("DB Config: %s", true), $useDbConfig));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if ($fullTableName !== Inflector::tableize($currentModelName)) {
|
2009-05-05 04:43:29 +00:00
|
|
|
$this->out(sprintf(__("DB Table: %s", true), $fullTableName));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if ($primaryKey != 'id') {
|
2009-05-05 04:43:29 +00:00
|
|
|
$this->out(sprintf(__("Primary Key: %s", true), $primaryKey));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if (!empty($validate)) {
|
2009-05-05 04:43:29 +00:00
|
|
|
$this->out(sprintf(__("Validation: %s", true), print_r($validate, true)));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if (!empty($associations)) {
|
2009-05-05 04:43:29 +00:00
|
|
|
$this->out(__("Associations:", true));
|
|
|
|
$assocKeys = array('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany');
|
|
|
|
foreach ($assocKeys as $assocKey) {
|
2009-05-08 05:09:19 +00:00
|
|
|
$this->_printAssociation($currentModelName, $assocKey, $associations);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-05-05 04:43:29 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->hr();
|
|
|
|
$looksGood = $this->in(__('Look okay?', true), array('y','n'), 'y');
|
2009-05-05 04:43:29 +00:00
|
|
|
|
|
|
|
if (strtolower($looksGood) == 'y') {
|
2009-05-05 04:10:24 +00:00
|
|
|
if ($this->bake($currentModelName, $associations, $validate, $primaryKey, $useTable, $this->connection)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->_checkUnitTest()) {
|
|
|
|
$this->bakeTest($currentModelName, $useTable, $associations);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2009-05-08 04:49:26 +00:00
|
|
|
|
2009-05-05 04:43:29 +00:00
|
|
|
/**
|
|
|
|
* Print out all the associations of a particular type
|
|
|
|
*
|
|
|
|
* @param string $modelName Name of the model relations belong to.
|
|
|
|
* @param string $type Name of association you want to see. i.e. 'belongsTo'
|
|
|
|
* @param string $associations Collection of associations.
|
2009-05-08 05:09:19 +00:00
|
|
|
* @access protected
|
2009-05-05 04:43:29 +00:00
|
|
|
* @return void
|
|
|
|
**/
|
2009-05-08 05:09:19 +00:00
|
|
|
function _printAssociation($modelName, $type, $associations) {
|
2009-05-05 04:43:29 +00:00
|
|
|
if (!empty($associations[$type])) {
|
|
|
|
for ($i = 0; $i < count($associations[$type]); $i++) {
|
|
|
|
$out = "\t" . $modelName . ' ' . $type . ' ' . $associations[$type][$i]['alias'];
|
|
|
|
$this->out($out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-08 04:49:26 +00:00
|
|
|
|
2009-05-05 04:43:29 +00:00
|
|
|
/**
|
|
|
|
* Finds a primary Key in a list of fields.
|
|
|
|
*
|
|
|
|
* @param array $fields Array of fields that might have a primary key.
|
|
|
|
* @return string Name of field that is a primary key.
|
2009-05-08 04:49:26 +00:00
|
|
|
* @access public
|
2009-05-05 04:43:29 +00:00
|
|
|
**/
|
|
|
|
function findPrimaryKey($fields) {
|
|
|
|
foreach ($fields as $name => $field) {
|
|
|
|
if (isset($field['key']) && $field['key'] == 'primary') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->in(__('What is the primaryKey?', true), null, $name);
|
|
|
|
}
|
2009-05-08 04:49:26 +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
|
|
|
*
|
|
|
|
* @param object $model
|
|
|
|
* @param boolean $interactive
|
|
|
|
* @return array $validate
|
|
|
|
* @access public
|
|
|
|
*/
|
2009-05-05 04:43:29 +00:00
|
|
|
function doValidation(&$model) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!is_object($model)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$fields = $model->schema();
|
|
|
|
|
|
|
|
if (empty($fields)) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-05-08 04:49:26 +00:00
|
|
|
$validate = array();
|
|
|
|
$this->initValidations();
|
|
|
|
foreach ($fields as $fieldName => $field) {
|
|
|
|
$validation = $this->fieldValidation($fieldName, $field, $model->primaryKey);
|
|
|
|
if (!empty($validation)) {
|
|
|
|
$validate[$fieldName] = $validation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $validate;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Populate the __validations array
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function initValidations() {
|
|
|
|
$options = $choices = array();
|
2008-05-30 11:40:08 +00:00
|
|
|
if (class_exists('Validation')) {
|
|
|
|
$parent = get_class_methods(get_parent_class('Validation'));
|
|
|
|
$options = array_diff(get_class_methods('Validation'), $parent);
|
|
|
|
}
|
2009-05-08 04:49:26 +00:00
|
|
|
sort($options);
|
|
|
|
$default = 1;
|
|
|
|
foreach ($options as $key => $option) {
|
|
|
|
if ($option{0} != '_' && strtolower($option) != 'getinstance') {
|
|
|
|
$choices[$default] = strtolower($option);
|
|
|
|
$default++;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-05-08 04:49:26 +00:00
|
|
|
}
|
|
|
|
$this->__validations = $choices;
|
|
|
|
return $choices;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
**/
|
|
|
|
function fieldValidation($fieldName, $metaData, $primaryKey = 'id') {
|
|
|
|
$defaultChoice = count($this->__validations);
|
2009-05-09 04:35:03 +00:00
|
|
|
$validate = $alreadyChosen = array();
|
2009-05-08 04:49:26 +00:00
|
|
|
|
2009-05-08 05:09:19 +00:00
|
|
|
$anotherValidator = 'y';
|
|
|
|
while ($anotherValidator == 'y') {
|
|
|
|
if ($this->interactive) {
|
|
|
|
$this->out('');
|
|
|
|
$this->out(sprintf(__('Field: %s', true), $fieldName));
|
|
|
|
$this->out(sprintf(__('Type: %s', true), $metaData['type']));
|
|
|
|
$this->hr();
|
|
|
|
$this->out(__('Please select one of the following validation options:', true));
|
|
|
|
$this->hr();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-05-10 05:04:40 +00:00
|
|
|
|
2009-05-08 05:09:19 +00:00
|
|
|
$prompt = '';
|
|
|
|
for ($i = 1; $i < $defaultChoice; $i++) {
|
|
|
|
$prompt .= $i . ' - ' . $this->__validations[$i] . "\n";
|
|
|
|
}
|
|
|
|
$prompt .= sprintf(__("%s - Do not do any validation on this field.\n", true), $defaultChoice);
|
|
|
|
$prompt .= __("... or enter in a valid regex validation string.\n", true);
|
|
|
|
|
|
|
|
$methods = array_flip($this->__validations);
|
|
|
|
$guess = $defaultChoice;
|
|
|
|
if ($metaData['null'] != 1 && !in_array($fieldName, array($primaryKey, 'created', 'modified', 'updated'))) {
|
|
|
|
if ($fieldName == 'email') {
|
|
|
|
$guess = $methods['email'];
|
|
|
|
} elseif ($metaData['type'] == 'string') {
|
|
|
|
$guess = $methods['notempty'];
|
|
|
|
} elseif ($metaData['type'] == 'integer') {
|
|
|
|
$guess = $methods['numeric'];
|
|
|
|
} elseif ($metaData['type'] == 'boolean') {
|
|
|
|
$guess = $methods['numeric'];
|
|
|
|
} elseif ($metaData['type'] == 'datetime' || $metaData['type'] == 'date') {
|
|
|
|
$guess = $methods['date'];
|
|
|
|
} elseif ($metaData['type'] == 'time') {
|
|
|
|
$guess = $methods['time'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->interactive === true) {
|
|
|
|
$choice = $this->in($prompt, null, $guess);
|
2009-05-09 04:35:03 +00:00
|
|
|
if (in_array($choice, $alreadyChosen)) {
|
2009-05-09 05:00:14 +00:00
|
|
|
$this->out(__("You have already chosen that validation rule,\nplease choose again", true));
|
2009-05-09 04:35:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
2009-05-08 05:09:19 +00:00
|
|
|
$alreadyChosen[] = $choice;
|
|
|
|
} else {
|
|
|
|
$choice = $guess;
|
|
|
|
}
|
|
|
|
$validatorName = $this->__validations[$choice];
|
|
|
|
if ($choice != $defaultChoice) {
|
|
|
|
if (is_numeric($choice) && isset($this->__validations[$choice])) {
|
|
|
|
$validate[$validatorName] = $this->__validations[$choice];
|
|
|
|
} else {
|
|
|
|
$validate[$validatorName] = $choice;
|
|
|
|
}
|
|
|
|
}
|
2009-05-09 04:35:03 +00:00
|
|
|
if ($this->interactive == true && $choice != $defaultChoice) {
|
2009-05-08 05:09:19 +00:00
|
|
|
$anotherValidator = $this->in(__('Would you like to add another validation rule?', true), array('y', 'n'), 'n');
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2009-05-08 05:09:19 +00:00
|
|
|
$anotherValidator = 'n';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $validate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles associations
|
|
|
|
*
|
|
|
|
* @param object $model
|
|
|
|
* @return array $assocaitons
|
|
|
|
* @access public
|
|
|
|
*/
|
2009-05-05 04:43:29 +00:00
|
|
|
function doAssociations(&$model) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!is_object($model)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->out(__('One moment while the associations are detected.', true));
|
|
|
|
|
|
|
|
$fields = $model->schema();
|
|
|
|
if (empty($fields)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-05-05 04:43:29 +00:00
|
|
|
$associations = array(
|
|
|
|
'belongsTo' => array(), 'hasMany' => array(), 'hasOne'=> array(), 'hasAndBelongsToMany' => array()
|
|
|
|
);
|
2008-09-07 13:03:51 +00:00
|
|
|
$possibleKeys = array();
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-05-10 01:28:51 +00:00
|
|
|
$associations = $this->findBelongsTo($model, $associations);
|
|
|
|
$associations = $this->findHasOneAndMany($model, $associations);
|
|
|
|
$associations = $this->findHasAndBelongsToMany($model, $associations);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-05-05 04:43:29 +00:00
|
|
|
if ($this->interactive !== true) {
|
2008-05-30 11:40:08 +00:00
|
|
|
unset($associations['hasOne']);
|
|
|
|
}
|
|
|
|
|
2009-05-05 04:43:29 +00:00
|
|
|
if ($this->interactive === true) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->hr();
|
|
|
|
if (empty($associations)) {
|
|
|
|
$this->out(__('None found.', true));
|
|
|
|
} else {
|
|
|
|
$this->out(__('Please confirm the following associations:', true));
|
|
|
|
$this->hr();
|
2009-05-10 03:32:22 +00:00
|
|
|
$associations = $this->confirmAssociations($model, $associations);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-05-10 05:04:40 +00:00
|
|
|
$associations = $this->doMoreAssociations($model, $associations);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return $associations;
|
|
|
|
}
|
2009-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-05-10 01:28:51 +00:00
|
|
|
function findBelongsTo(&$model, $associations) {
|
2009-05-09 06:20:46 +00:00
|
|
|
$fields = $model->schema();
|
|
|
|
foreach ($fields as $fieldName => $field) {
|
|
|
|
$offset = strpos($fieldName, '_id');
|
2009-05-10 03:32:22 +00:00
|
|
|
if ($fieldName != $model->primaryKey && $fieldName != 'parent_id' && $offset !== false) {
|
2009-05-09 06:20:46 +00:00
|
|
|
$tmpModelName = $this->_modelNameFromKey($fieldName);
|
2009-05-09 06:22:29 +00:00
|
|
|
$associations['belongsTo'][] = array(
|
2009-05-09 06:20:46 +00:00
|
|
|
'alias' => $tmpModelName,
|
|
|
|
'className' => $tmpModelName,
|
|
|
|
'foreignKey' => $fieldName,
|
|
|
|
);
|
2009-05-10 03:32:22 +00:00
|
|
|
} elseif ($fieldName == 'parent_id') {
|
|
|
|
$associations['belongsTo'][] = array(
|
|
|
|
'alias' => 'Parent' . $model->name,
|
|
|
|
'className' => $model->name,
|
|
|
|
'foreignKey' => $fieldName,
|
|
|
|
);
|
2009-05-09 06:20:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $associations;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the hasOne and HasMany relations and add them to associations list
|
|
|
|
*
|
|
|
|
* @param object $model Model instance being generated
|
|
|
|
* @param array $associations Array of inprogress associations
|
|
|
|
* @return array $associations with hasOne and hasMany added in.
|
|
|
|
**/
|
2009-05-10 01:28:51 +00:00
|
|
|
function findHasOneAndMany(&$model, $associations) {
|
2009-05-09 06:20:46 +00:00
|
|
|
$foreignKey = $this->_modelKey($model->name);
|
|
|
|
foreach ($this->__tables as $otherTable) {
|
|
|
|
$tempOtherModel = $this->_getModelObject($this->_modelName($otherTable));
|
|
|
|
$modelFieldsTemp = $tempOtherModel->schema();
|
|
|
|
|
2009-05-10 01:28:51 +00:00
|
|
|
$pattern = '/_' . preg_quote($model->table, '/') . '|' . preg_quote($model->table, '/') . '_/';
|
|
|
|
$possibleJoinTable = preg_match($pattern , $otherTable);
|
2009-05-10 03:32:22 +00:00
|
|
|
if ($possibleJoinTable == true) {
|
|
|
|
continue;
|
|
|
|
}
|
2009-05-09 06:20:46 +00:00
|
|
|
foreach ($modelFieldsTemp as $fieldName => $field) {
|
2009-05-10 03:32:22 +00:00
|
|
|
$assoc = false;
|
|
|
|
if ($fieldName != $model->primaryKey && $fieldName == $foreignKey) {
|
2009-05-09 06:20:46 +00:00
|
|
|
$assoc = array(
|
|
|
|
'alias' => $tempOtherModel->name,
|
|
|
|
'className' => $tempOtherModel->name,
|
|
|
|
'foreignKey' => $fieldName
|
|
|
|
);
|
2009-05-10 03:32:22 +00:00
|
|
|
} elseif ($otherTable == $model->table && $fieldName == 'parent_id') {
|
|
|
|
$assoc = array(
|
|
|
|
'alias' => 'Child' . $model->name,
|
|
|
|
'className' => $model->name,
|
|
|
|
'foreignKey' => $fieldName
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if ($assoc) {
|
2009-05-09 06:20:46 +00:00
|
|
|
$associations['hasOne'][] = $assoc;
|
|
|
|
$associations['hasMany'][] = $assoc;
|
|
|
|
}
|
2009-05-10 03:32:22 +00:00
|
|
|
|
2009-05-09 06:20:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $associations;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the hasAndBelongsToMany relations and add them to associations list
|
|
|
|
*
|
|
|
|
* @param object $model Model instance being generated
|
|
|
|
* @param array $associations Array of inprogress associations
|
|
|
|
* @return array $associations with hasAndBelongsToMany added in.
|
|
|
|
**/
|
2009-05-10 01:28:51 +00:00
|
|
|
function findHasAndBelongsToMany(&$model, $associations) {
|
2009-05-09 06:20:46 +00:00
|
|
|
$foreignKey = $this->_modelKey($model->name);
|
|
|
|
foreach ($this->__tables as $otherTable) {
|
|
|
|
$tempOtherModel = $this->_getModelObject($this->_modelName($otherTable));
|
|
|
|
$modelFieldsTemp = $tempOtherModel->schema();
|
|
|
|
|
|
|
|
$offset = strpos($otherTable, $model->table . '_');
|
|
|
|
$otherOffset = strpos($otherTable, '_' . $model->table);
|
|
|
|
|
|
|
|
if ($offset !== false) {
|
|
|
|
$offset = strlen($model->table . '_');
|
|
|
|
$habtmName = $this->_modelName(substr($otherTable, $offset));
|
2009-05-09 06:22:29 +00:00
|
|
|
$associations['hasAndBelongsToMany'][] = array(
|
2009-05-09 06:20:46 +00:00
|
|
|
'alias' => $habtmName,
|
|
|
|
'className' => $habtmName,
|
|
|
|
'foreignKey' => $foreignKey,
|
|
|
|
'associationForeignKey' => $this->_modelKey($habtmName),
|
|
|
|
'joinTable' => $otherTable
|
|
|
|
);
|
|
|
|
} elseif ($otherOffset !== false) {
|
|
|
|
$habtmName = $this->_modelName(substr($otherTable, 0, $otherOffset));
|
2009-05-09 06:22:29 +00:00
|
|
|
$associations['hasAndBelongsToMany'][] = array(
|
2009-05-09 06:20:46 +00:00
|
|
|
'alias' => $habtmName,
|
|
|
|
'className' => $habtmName,
|
|
|
|
'foreignKey' => $foreignKey,
|
|
|
|
'associationForeignKey' => $this->_modelKey($habtmName),
|
|
|
|
'joinTable' => $otherTable
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $associations;
|
|
|
|
}
|
2009-05-10 05:04:40 +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
|
|
|
|
**/
|
|
|
|
function confirmAssociations(&$model, $associations) {
|
|
|
|
foreach ($associations as $type => $settings) {
|
|
|
|
if (!empty($associations[$type])) {
|
|
|
|
$count = count($associations[$type]);
|
|
|
|
$response = 'y';
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
|
|
$prompt = "{$model->name} {$type} {$associations[$type][$i]['alias']}";
|
|
|
|
$response = $this->in("{$prompt}?", array('y','n'), 'y');
|
|
|
|
|
|
|
|
if ('n' == low($response)) {
|
|
|
|
unset($associations[$type][$i]);
|
|
|
|
} elseif ($type == 'hasMany') {
|
|
|
|
unset($associations['hasOne'][$i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$associations[$type] = array_merge($associations[$type]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $associations;
|
|
|
|
}
|
2009-05-09 06:20:46 +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.
|
|
|
|
**/
|
|
|
|
function doMoreAssociations($model, $associations) {
|
|
|
|
$prompt = __('Would you like to define some additional model associations?', true);
|
|
|
|
$wannaDoMoreAssoc = $this->in($prompt, array('y','n'), 'n');
|
|
|
|
$possibleKeys = $this->_generatePossibleKeys();
|
|
|
|
while (low($wannaDoMoreAssoc) == 'y') {
|
2009-05-10 05:35:59 +00:00
|
|
|
$assocs = array('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany');
|
2009-05-10 05:04:40 +00:00
|
|
|
$this->out(__('What is the association type?', true));
|
2009-05-10 05:35:59 +00:00
|
|
|
$assocType = intval($this->inOptions($assocs, __('Enter a number',true)));
|
|
|
|
|
2009-05-10 05:04:40 +00:00
|
|
|
$this->out(__("For the following options be very careful to match your setup exactly.\nAny spelling mistakes will cause errors.", true));
|
|
|
|
$this->hr();
|
|
|
|
|
|
|
|
$alias = $this->in(__('What is the alias for this association?', true));
|
|
|
|
$className = $this->in(sprintf(__('What className will %s use?', true), $alias), null, $alias );
|
|
|
|
$suggestedForeignKey = null;
|
|
|
|
|
2009-05-10 05:35:59 +00:00
|
|
|
if ($assocType == 0) {
|
2009-05-10 05:04:40 +00:00
|
|
|
$showKeys = $possibleKeys[$model->table];
|
|
|
|
$suggestedForeignKey = $this->_modelKey($alias);
|
|
|
|
} else {
|
|
|
|
$otherTable = Inflector::tableize($className);
|
|
|
|
if (in_array($otherTable, $this->__tables)) {
|
2009-05-10 05:35:59 +00:00
|
|
|
if ($assocType < 3) {
|
2009-05-10 05:04:40 +00:00
|
|
|
$showKeys = $possibleKeys[$otherTable];
|
|
|
|
} else {
|
|
|
|
$showKeys = null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$otherTable = $this->in(__('What is the table for this model?', true));
|
|
|
|
$showKeys = $possibleKeys[$otherTable];
|
|
|
|
}
|
|
|
|
$suggestedForeignKey = $this->_modelKey($model->name);
|
|
|
|
}
|
|
|
|
if (!empty($showKeys)) {
|
|
|
|
$this->out(__('A helpful List of possible keys', true));
|
2009-05-10 05:35:59 +00:00
|
|
|
$foreignKey = $this->inOptions($showKeys, __('What is the foreignKey?', true));
|
|
|
|
$foreignKey = $showKeys[intval($foreignKey)];
|
2009-05-10 05:04:40 +00:00
|
|
|
}
|
|
|
|
if (!isset($foreignKey)) {
|
|
|
|
$foreignKey = $this->in(__('What is the foreignKey? Specify your own.', true), null, $suggestedForeignKey);
|
|
|
|
}
|
2009-05-10 05:35:59 +00:00
|
|
|
if ($assocType == 3) {
|
2009-05-10 05:04:40 +00:00
|
|
|
$associationForeignKey = $this->in(__('What is the associationForeignKey?', true), null, $this->_modelKey($model->name));
|
|
|
|
$joinTable = $this->in(__('What is the joinTable?', true));
|
|
|
|
}
|
|
|
|
$associations[$assocs[$assocType]] = array_values((array)$associations[$assocs[$assocType]]);
|
|
|
|
$count = count($associations[$assocs[$assocType]]);
|
|
|
|
$i = ($count > 0) ? $count : 0;
|
|
|
|
$associations[$assocs[$assocType]][$i]['alias'] = $alias;
|
|
|
|
$associations[$assocs[$assocType]][$i]['className'] = $className;
|
|
|
|
$associations[$assocs[$assocType]][$i]['foreignKey'] = $foreignKey;
|
2009-05-10 05:35:59 +00:00
|
|
|
if ($assocType == 3) {
|
2009-05-10 05:04:40 +00:00
|
|
|
$associations[$assocs[$assocType]][$i]['associationForeignKey'] = $associationForeignKey;
|
|
|
|
$associations[$assocs[$assocType]][$i]['joinTable'] = $joinTable;
|
|
|
|
}
|
|
|
|
$wannaDoMoreAssoc = $this->in(__('Define another association?', true), array('y','n'), 'y');
|
|
|
|
}
|
|
|
|
return $associations;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds all possible keys to use on custom associations.
|
|
|
|
*
|
|
|
|
* @return array array of tables and possible keys
|
|
|
|
**/
|
|
|
|
function _generatePossibleKeys() {
|
|
|
|
$possible = array();
|
|
|
|
foreach ($this->__tables as $otherTable) {
|
|
|
|
$tempOtherModel = & new Model(array('table' => $otherTable, 'ds' => $this->connection));
|
|
|
|
$modelFieldsTemp = $tempOtherModel->schema();
|
|
|
|
foreach ($modelFieldsTemp as $fieldName => $field) {
|
|
|
|
if ($field['type'] == 'integer' || $field['type'] == 'string') {
|
|
|
|
$possible[$otherTable][] = $fieldName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $possible;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Assembles and writes a Model file.
|
|
|
|
*
|
|
|
|
* @param mixed $name Model name or object
|
|
|
|
* @param mixed $associations if array and $name is not an object assume Model associations array otherwise boolean interactive
|
|
|
|
* @param array $validate Validation rules
|
|
|
|
* @param string $primaryKey Primary key to use
|
|
|
|
* @param string $useTable Table to use
|
|
|
|
* @param string $useDbConfig Database configuration setting to use
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function bake($name, $associations = array(), $validate = array(), $primaryKey = 'id', $useTable = null, $useDbConfig = 'default') {
|
|
|
|
|
|
|
|
if (is_object($name)) {
|
|
|
|
if (!is_array($associations)) {
|
|
|
|
$associations = $this->doAssociations($name, $associations);
|
2009-05-05 04:43:29 +00:00
|
|
|
$validate = $this->doValidation($name);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$primaryKey = $name->primaryKey;
|
|
|
|
$useTable = $name->table;
|
|
|
|
$useDbConfig = $name->useDbConfig;
|
|
|
|
$name = $name->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
$out = "<?php\n";
|
|
|
|
$out .= "class {$name} extends {$this->plugin}AppModel {\n\n";
|
|
|
|
$out .= "\tvar \$name = '{$name}';\n";
|
|
|
|
|
|
|
|
if ($useDbConfig !== 'default') {
|
|
|
|
$out .= "\tvar \$useDbConfig = '$useDbConfig';\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($useTable && $useTable !== Inflector::tableize($name)) || $useTable === false) {
|
|
|
|
$table = "'$useTable'";
|
|
|
|
if (!$useTable) {
|
|
|
|
$table = 'false';
|
|
|
|
}
|
|
|
|
$out .= "\tvar \$useTable = $table;\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($primaryKey !== 'id') {
|
|
|
|
$out .= "\tvar \$primaryKey = '$primaryKey';\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$validateCount = count($validate);
|
|
|
|
if (is_array($validate) && $validateCount > 0) {
|
|
|
|
$out .= "\tvar \$validate = array(\n";
|
|
|
|
$keys = array_keys($validate);
|
|
|
|
for ($i = 0; $i < $validateCount; $i++) {
|
2008-07-31 23:58:20 +00:00
|
|
|
$val = "'" . $validate[$keys[$i]] . "'";
|
|
|
|
$out .= "\t\t'" . $keys[$i] . "' => array({$val})";
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($i + 1 < $validateCount) {
|
|
|
|
$out .= ",";
|
|
|
|
}
|
|
|
|
$out .= "\n";
|
|
|
|
}
|
|
|
|
$out .= "\t);\n";
|
|
|
|
}
|
|
|
|
$out .= "\n";
|
|
|
|
|
|
|
|
if (!empty($associations)) {
|
2008-08-07 17:06:03 +00:00
|
|
|
if (!empty($associations['belongsTo']) || !empty($associations['hasOne']) || !empty($associations['hasMany']) || !empty($associations['hasAndBelongsToMany'])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$out.= "\t//The Associations below have been created with all possible keys, those that are not needed can be removed\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($associations['belongsTo'])) {
|
|
|
|
$out .= "\tvar \$belongsTo = array(\n";
|
|
|
|
$belongsToCount = count($associations['belongsTo']);
|
|
|
|
|
|
|
|
for ($i = 0; $i < $belongsToCount; $i++) {
|
2009-03-08 18:05:18 +00:00
|
|
|
$out .= "\t\t'{$associations['belongsTo'][$i]['alias']}' => array(\n";
|
|
|
|
$out .= "\t\t\t'className' => '{$associations['belongsTo'][$i]['className']}',\n";
|
|
|
|
$out .= "\t\t\t'foreignKey' => '{$associations['belongsTo'][$i]['foreignKey']}',\n";
|
|
|
|
$out .= "\t\t\t'conditions' => '',\n";
|
|
|
|
$out .= "\t\t\t'fields' => '',\n";
|
|
|
|
$out .= "\t\t\t'order' => ''\n";
|
|
|
|
$out .= "\t\t)";
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($i + 1 < $belongsToCount) {
|
|
|
|
$out .= ",";
|
|
|
|
}
|
|
|
|
$out .= "\n";
|
|
|
|
|
|
|
|
}
|
|
|
|
$out .= "\t);\n\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($associations['hasOne'])) {
|
|
|
|
$out .= "\tvar \$hasOne = array(\n";
|
|
|
|
$hasOneCount = count($associations['hasOne']);
|
|
|
|
|
|
|
|
for ($i = 0; $i < $hasOneCount; $i++) {
|
2009-03-08 18:05:18 +00:00
|
|
|
$out .= "\t\t'{$associations['hasOne'][$i]['alias']}' => array(\n";
|
|
|
|
$out .= "\t\t\t'className' => '{$associations['hasOne'][$i]['className']}',\n";
|
|
|
|
$out .= "\t\t\t'foreignKey' => '{$associations['hasOne'][$i]['foreignKey']}',\n";
|
|
|
|
$out .= "\t\t\t'dependent' => false,\n";
|
|
|
|
$out .= "\t\t\t'conditions' => '',\n";
|
|
|
|
$out .= "\t\t\t'fields' => '',\n";
|
|
|
|
$out .= "\t\t\t'order' => ''\n";
|
|
|
|
$out .= "\t\t)";
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($i + 1 < $hasOneCount) {
|
|
|
|
$out .= ",";
|
|
|
|
}
|
|
|
|
$out .= "\n";
|
|
|
|
|
|
|
|
}
|
|
|
|
$out .= "\t);\n\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($associations['hasMany'])) {
|
|
|
|
$out .= "\tvar \$hasMany = array(\n";
|
|
|
|
$hasManyCount = count($associations['hasMany']);
|
|
|
|
|
|
|
|
for ($i = 0; $i < $hasManyCount; $i++) {
|
2009-03-08 18:05:18 +00:00
|
|
|
$out .= "\t\t'{$associations['hasMany'][$i]['alias']}' => array(\n";
|
|
|
|
$out .= "\t\t\t'className' => '{$associations['hasMany'][$i]['className']}',\n";
|
|
|
|
$out .= "\t\t\t'foreignKey' => '{$associations['hasMany'][$i]['foreignKey']}',\n";
|
|
|
|
$out .= "\t\t\t'dependent' => false,\n";
|
|
|
|
$out .= "\t\t\t'conditions' => '',\n";
|
|
|
|
$out .= "\t\t\t'fields' => '',\n";
|
|
|
|
$out .= "\t\t\t'order' => '',\n";
|
|
|
|
$out .= "\t\t\t'limit' => '',\n";
|
|
|
|
$out .= "\t\t\t'offset' => '',\n";
|
|
|
|
$out .= "\t\t\t'exclusive' => '',\n";
|
|
|
|
$out .= "\t\t\t'finderQuery' => '',\n";
|
|
|
|
$out .= "\t\t\t'counterQuery' => ''\n";
|
|
|
|
$out .= "\t\t)";
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($i + 1 < $hasManyCount) {
|
|
|
|
$out .= ",";
|
|
|
|
}
|
|
|
|
$out .= "\n";
|
|
|
|
}
|
|
|
|
$out .= "\t);\n\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($associations['hasAndBelongsToMany'])) {
|
|
|
|
$out .= "\tvar \$hasAndBelongsToMany = array(\n";
|
|
|
|
$hasAndBelongsToManyCount = count($associations['hasAndBelongsToMany']);
|
|
|
|
|
|
|
|
for ($i = 0; $i < $hasAndBelongsToManyCount; $i++) {
|
2009-03-08 18:05:18 +00:00
|
|
|
$out .= "\t\t'{$associations['hasAndBelongsToMany'][$i]['alias']}' => array(\n";
|
|
|
|
$out .= "\t\t\t'className' => '{$associations['hasAndBelongsToMany'][$i]['className']}',\n";
|
|
|
|
$out .= "\t\t\t'joinTable' => '{$associations['hasAndBelongsToMany'][$i]['joinTable']}',\n";
|
|
|
|
$out .= "\t\t\t'foreignKey' => '{$associations['hasAndBelongsToMany'][$i]['foreignKey']}',\n";
|
|
|
|
$out .= "\t\t\t'associationForeignKey' => '{$associations['hasAndBelongsToMany'][$i]['associationForeignKey']}',\n";
|
|
|
|
$out .= "\t\t\t'unique' => true,\n";
|
|
|
|
$out .= "\t\t\t'conditions' => '',\n";
|
|
|
|
$out .= "\t\t\t'fields' => '',\n";
|
|
|
|
$out .= "\t\t\t'order' => '',\n";
|
|
|
|
$out .= "\t\t\t'limit' => '',\n";
|
|
|
|
$out .= "\t\t\t'offset' => '',\n";
|
|
|
|
$out .= "\t\t\t'finderQuery' => '',\n";
|
|
|
|
$out .= "\t\t\t'deleteQuery' => '',\n";
|
|
|
|
$out .= "\t\t\t'insertQuery' => ''\n";
|
|
|
|
$out .= "\t\t)";
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($i + 1 < $hasAndBelongsToManyCount) {
|
|
|
|
$out .= ",";
|
|
|
|
}
|
|
|
|
$out .= "\n";
|
|
|
|
}
|
|
|
|
$out .= "\t);\n\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$out .= "}\n";
|
|
|
|
$out .= "?>";
|
|
|
|
$filename = $this->path . Inflector::underscore($name) . '.php';
|
|
|
|
$this->out("\nBaking model class for $name...");
|
|
|
|
return $this->createFile($filename, $out);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assembles and writes a unit test file
|
|
|
|
*
|
|
|
|
* @param string $className Model class name
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function bakeTest($className, $useTable = null, $associations = array()) {
|
2009-04-30 01:18:08 +00:00
|
|
|
$this->fixture($className, $useTable);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-04-30 01:18:08 +00:00
|
|
|
$fixtureInc = 'app';
|
|
|
|
if ($this->plugin) {
|
|
|
|
$fixtureInc = 'plugin.'.Inflector::underscore($this->plugin);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-04-30 01:18:08 +00:00
|
|
|
$fixture[] = "'{$fixtureInc}." . Inflector::underscore($className) ."'";
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-04-30 01:18:08 +00:00
|
|
|
if (!empty($associations)) {
|
|
|
|
$assoc[] = Set::extract($associations, 'belongsTo.{n}.className');
|
|
|
|
$assoc[] = Set::extract($associations, 'hasOne.{n}.className');
|
|
|
|
$assoc[] = Set::extract($associations, 'hasMany.{n}.className');
|
|
|
|
foreach ($assoc as $key => $value) {
|
|
|
|
if (is_array($value)) {
|
|
|
|
foreach ($value as $class) {
|
|
|
|
$fixture[] = "'{$fixtureInc}." . Inflector::underscore($class) ."'";
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-04-30 01:18:08 +00:00
|
|
|
}
|
|
|
|
$fixture = join(", ", $fixture);
|
|
|
|
|
|
|
|
$import = $className;
|
|
|
|
if (isset($this->plugin)) {
|
|
|
|
$import = $this->plugin . '.' . $className;
|
|
|
|
}
|
|
|
|
|
|
|
|
$out = "App::import('Model', '$import');\n\n";
|
|
|
|
$out .= "class {$className}TestCase extends CakeTestCase {\n";
|
|
|
|
$out .= "\tvar \${$className} = null;\n";
|
|
|
|
$out .= "\tvar \$fixtures = array($fixture);\n\n";
|
|
|
|
$out .= "\tfunction startTest() {\n";
|
|
|
|
$out .= "\t\t\$this->{$className} =& ClassRegistry::init('{$className}');\n";
|
|
|
|
$out .= "\t}\n\n";
|
|
|
|
$out .= "\tfunction endTest() {\n";
|
|
|
|
$out .= "\t\tunset(\$this->{$className});\n";
|
|
|
|
$out .= "\t}\n\n";
|
|
|
|
$out .= "\tfunction test{$className}Instance() {\n";
|
|
|
|
$out .= "\t\t\$this->assertTrue(is_a(\$this->{$className}, '{$className}'));\n";
|
|
|
|
$out .= "\t}\n\n";
|
|
|
|
$out .= "}\n";
|
|
|
|
|
|
|
|
$path = MODEL_TESTS;
|
|
|
|
if (isset($this->plugin)) {
|
|
|
|
$pluginPath = 'plugins' . DS . Inflector::underscore($this->plugin) . DS;
|
|
|
|
$path = APP . $pluginPath . 'tests' . DS . 'cases' . DS . 'models' . DS;
|
|
|
|
}
|
|
|
|
|
|
|
|
$filename = Inflector::underscore($className).'.test.php';
|
|
|
|
$this->out("\nBaking unit test for $className...");
|
|
|
|
|
|
|
|
$header = '$Id';
|
|
|
|
$content = "<?php \n/* SVN FILE: $header$ */\n/* ". $className ." Test cases generated on: " . date('Y-m-d H:m:s') . " : ". time() . "*/\n{$out}?>";
|
|
|
|
return $this->createFile($path . $filename, $content);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-05-05 04:10:24 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* outputs the a list of possible models or controllers from database
|
|
|
|
*
|
|
|
|
* @param string $useDbConfig Database configuration name
|
|
|
|
* @access public
|
|
|
|
*/
|
2009-05-05 04:10:24 +00:00
|
|
|
function listAll($useDbConfig = null, $interactive = true) {
|
|
|
|
if (!isset($useDbConfig)) {
|
|
|
|
$useDbConfig = $this->connection;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
$db =& ConnectionManager::getDataSource($useDbConfig);
|
|
|
|
$usePrefix = empty($db->config['prefix']) ? '' : $db->config['prefix'];
|
|
|
|
if ($usePrefix) {
|
|
|
|
$tables = array();
|
|
|
|
foreach ($db->listSources() as $table) {
|
|
|
|
if (!strncmp($table, $usePrefix, strlen($usePrefix))) {
|
|
|
|
$tables[] = substr($table, strlen($usePrefix));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$tables = $db->listSources();
|
|
|
|
}
|
|
|
|
if (empty($tables)) {
|
|
|
|
$this->err(__('Your database does not have any tables.', true));
|
2008-06-04 19:04:58 +00:00
|
|
|
$this->_stop();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->__tables = $tables;
|
|
|
|
|
|
|
|
if ($interactive === true) {
|
|
|
|
$this->out(__('Possible Models based on your current database:', true));
|
|
|
|
$this->_modelNames = array();
|
|
|
|
$count = count($tables);
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
|
|
$this->_modelNames[] = $this->_modelName($tables[$i]);
|
|
|
|
$this->out($i + 1 . ". " . $this->_modelNames[$i]);
|
|
|
|
}
|
|
|
|
}
|
2009-04-30 02:50:43 +00:00
|
|
|
return $this->__tables;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-05-10 05:04:40 +00:00
|
|
|
|
2009-04-29 01:20:34 +00:00
|
|
|
/**
|
|
|
|
* Interact with the user to determine the table name of a particular model
|
|
|
|
*
|
|
|
|
* @param string $modelName Name of the model you want a table for.
|
|
|
|
* @param string $useDbConfig Name of the database config you want to get tables from.
|
|
|
|
* @return void
|
|
|
|
**/
|
2009-05-05 04:10:24 +00:00
|
|
|
function getTable($modelName, $useDbConfig = null) {
|
|
|
|
if (!isset($useDbConfig)) {
|
|
|
|
$useDbConfig = $this->connection;
|
|
|
|
}
|
2009-04-29 01:20:34 +00:00
|
|
|
$db =& ConnectionManager::getDataSource($useDbConfig);
|
|
|
|
$useTable = Inflector::tableize($modelName);
|
|
|
|
$fullTableName = $db->fullTableName($useTable, false);
|
|
|
|
$tableIsGood = false;
|
|
|
|
|
|
|
|
if (array_search($useTable, $this->__tables) === false) {
|
|
|
|
$this->out('');
|
2009-05-09 05:00:14 +00:00
|
|
|
$this->out(sprintf(__("Given your model named '%s',\nCake would expect a database table named '%s'", true), $modelName, $fullTableName));
|
2009-04-29 01:20:34 +00:00
|
|
|
$tableIsGood = $this->in(__('Do you want to use this table?', true), array('y','n'), 'y');
|
|
|
|
}
|
2009-05-10 16:43:38 +00:00
|
|
|
if (strtolower($tableIsGood) == 'n') {
|
2009-04-29 01:20:34 +00:00
|
|
|
$useTable = $this->in(__('What is the name of the table (enter "null" to use NO table)?', true));
|
|
|
|
}
|
|
|
|
return $useTable;
|
|
|
|
}
|
2009-05-10 05:04:40 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Forces the user to specify the model he wants to bake, and returns the selected model name.
|
|
|
|
*
|
|
|
|
* @return string the model name
|
|
|
|
* @access public
|
|
|
|
*/
|
2009-05-05 04:10:24 +00:00
|
|
|
function getName($useDbConfig = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->listAll($useDbConfig);
|
|
|
|
|
|
|
|
$enteredModel = '';
|
|
|
|
|
|
|
|
while ($enteredModel == '') {
|
2009-05-09 05:00:14 +00:00
|
|
|
$enteredModel = $this->in(__("Enter a number from the list above,\ntype in the name of another model, or 'q' to exit", true), null, 'q');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($enteredModel === 'q') {
|
|
|
|
$this->out(__("Exit", true));
|
2008-06-04 19:04:58 +00:00
|
|
|
$this->_stop();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($enteredModel == '' || intval($enteredModel) > count($this->_modelNames)) {
|
2009-05-09 05:00:14 +00:00
|
|
|
$this->err(__("The model name you supplied was empty,\nor the number you selected was not an option. Please try again.", true));
|
2008-05-30 11:40:08 +00:00
|
|
|
$enteredModel = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (intval($enteredModel) > 0 && intval($enteredModel) <= count($this->_modelNames)) {
|
|
|
|
$currentModelName = $this->_modelNames[intval($enteredModel) - 1];
|
|
|
|
} else {
|
|
|
|
$currentModelName = $enteredModel;
|
|
|
|
}
|
|
|
|
return $currentModelName;
|
|
|
|
}
|
2009-05-09 06:20:46 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Displays help contents
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function help() {
|
|
|
|
$this->hr();
|
|
|
|
$this->out("Usage: cake bake model <arg1>");
|
|
|
|
$this->hr();
|
|
|
|
$this->out('Commands:');
|
|
|
|
$this->out("\n\tmodel\n\t\tbakes model in interactive mode.");
|
|
|
|
$this->out("\n\tmodel <name>\n\t\tbakes model file with no associations or validation");
|
2009-04-28 03:43:06 +00:00
|
|
|
$this->out("\n\tmodel all\n\t\tbakes all model files with associations and validation");
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->out("");
|
2008-06-04 19:04:58 +00:00
|
|
|
$this->_stop();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-05-09 06:20:46 +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-04-30 01:18:08 +00:00
|
|
|
* @return null.
|
|
|
|
**/
|
|
|
|
function fixture($className, $useTable = null) {
|
2009-05-05 04:10:24 +00:00
|
|
|
$this->Fixture->connection = $this->connection;
|
2009-04-30 01:18:08 +00:00
|
|
|
$this->Fixture->bake($className, $useTable);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|