📄 model.php
字号:
<?php/* SVN FILE: $Id: model.php 7118 2008-06-04 20:49:29Z gwoo $ *//** * The ModelTask handles creating and updating models files. * * Long description for file * * PHP versions 4 and 5 * * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/> * Copyright 2005-2008, Cake Software Foundation, Inc. * 1785 E. Sahara Avenue, Suite 490-204 * Las Vegas, Nevada 89104 * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * * @filesource * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. * @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: 7118 $ * @modifiedby $LastChangedBy: gwoo $ * @lastmodified $Date: 2008-06-04 13:49:29 -0700 (Wed, 04 Jun 2008) $ * @license http://www.opensource.org/licenses/mit-license.php The MIT License *//** * Task class for creating and updating model files. * * @package cake * @subpackage cake.cake.console.libs.tasks */class ModelTask extends Shell {/** * Name of plugin * * @var string * @access public */ var $plugin = null;/** * path to MODELS directory * * @var string * @access public */ var $path = MODELS;/** * tasks * * @var array * @access public */ var $tasks = array('DbConfig');/** * Execution method always used for tasks * * @access public */ function execute() { if (empty($this->args)) { $this->__interactive(); } if (!empty($this->args[0])) { $model = Inflector::camelize($this->args[0]); if ($this->bake($model)) { if ($this->_checkUnitTest()) { $this->bakeTest($model); } } } }/** * Handles interactive baking * * @access private */ function __interactive() { $this->hr(); $this->out(sprintf("Bake Model\nPath: %s", $this->path)); $this->hr(); $this->interactive = true; $useTable = null; $primaryKey = 'id'; $validate = array(); $associations = array('belongsTo'=> array(), 'hasOne'=> array(), 'hasMany' => array(), 'hasAndBelongsToMany'=> array()); $useDbConfig = 'default'; $configs = get_class_vars('DATABASE_CONFIG'); if (!is_array($configs)) { return $this->DbConfig->execute(); } $connections = array_keys($configs); if (count($connections) > 1) { $useDbConfig = $this->in(__('Use Database Config', true) .':', $connections, 'default'); } $currentModelName = $this->getName($useDbConfig); $db =& ConnectionManager::getDataSource($useDbConfig); $useTable = Inflector::tableize($currentModelName); $fullTableName = $db->fullTableName($useTable, false); $tableIsGood = false; if (array_search($useTable, $this->__tables) === false) { $this->out(''); $this->out(sprintf(__("Given your model named '%s', Cake would expect a database table named %s", true), $currentModelName, $fullTableName)); $tableIsGood = $this->in(__('Do you want to use this table?', true), array('y','n'), 'y'); } if (low($tableIsGood) == 'n' || low($tableIsGood) == 'no') { $useTable = $this->in(__('What is the name of the table (enter "null" to use NO table)?', true)); } while ($tableIsGood == false && low($useTable) != 'null') { if (is_array($this->__tables) && !in_array($useTable, $this->__tables)) { $fullTableName = $db->fullTableName($useTable, false); $this->out($fullTableName . ' does not exist.'); $useTable = $this->in(__('What is the name of the table (enter "null" to use NO table)?', true)); $tableIsGood = false; } else { $tableIsGood = true; } } $wannaDoValidation = $this->in(__('Would you like to supply validation criteria for the fields in your model?', true), array('y','n'), 'y'); if (in_array($useTable, $this->__tables)) { App::import('Model'); $tempModel = new Model(array('name' => $currentModelName, 'table' => $useTable, 'ds' => $useDbConfig)); $fields = $tempModel->schema(); if (!array_key_exists('id', $fields)) { foreach ($fields as $name => $field) { if (isset($field['key']) && $field['key'] == 'primary') { break; } } $primaryKey = $this->in(__('What is the primaryKey?', true), null, $name); } } if (array_search($useTable, $this->__tables) !== false && (low($wannaDoValidation) == 'y' || low($wannaDoValidation) == 'yes')) { $validate = $this->doValidation($tempModel); } $wannaDoAssoc = $this->in(__('Would you like to define model associations (hasMany, hasOne, belongsTo, etc.)?', true), array('y','n'), 'y'); if ((low($wannaDoAssoc) == 'y' || low($wannaDoAssoc) == 'yes')) { $associations = $this->doAssociations($tempModel); } $this->out(''); $this->hr(); $this->out(__('The following Model will be created:', true)); $this->hr(); $this->out("Name: " . $currentModelName); if ($useDbConfig !== 'default') { $this->out("DB Config: " . $useDbConfig); } if ($fullTableName !== Inflector::tableize($currentModelName)) { $this->out("DB Table: " . $fullTableName); } if ($primaryKey != 'id') { $this->out("Primary Key: " . $primaryKey); } if (!empty($validate)) { $this->out("Validation: " . print_r($validate, true)); } if (!empty($associations)) { $this->out("Associations:"); if (!empty($associations['belongsTo'])) { for ($i = 0; $i < count($associations['belongsTo']); $i++) { $this->out(" $currentModelName belongsTo {$associations['belongsTo'][$i]['alias']}"); } } if (!empty($associations['hasOne'])) { for ($i = 0; $i < count($associations['hasOne']); $i++) { $this->out(" $currentModelName hasOne {$associations['hasOne'][$i]['alias']}"); } } if (!empty($associations['hasMany'])) { for ($i = 0; $i < count($associations['hasMany']); $i++) { $this->out(" $currentModelName hasMany {$associations['hasMany'][$i]['alias']}"); } } if (!empty($associations['hasAndBelongsToMany'])) { for ($i = 0; $i < count($associations['hasAndBelongsToMany']); $i++) { $this->out(" $currentModelName hasAndBelongsToMany {$associations['hasAndBelongsToMany'][$i]['alias']}"); } } } $this->hr(); $looksGood = $this->in(__('Look okay?', true), array('y','n'), 'y'); if (low($looksGood) == 'y' || low($looksGood) == 'yes') { if ($this->bake($currentModelName, $associations, $validate, $primaryKey, $useTable, $useDbConfig)) { if ($this->_checkUnitTest()) { $this->bakeTest($currentModelName, $useTable, $associations); } } } else { return false; } }/** * Handles associations * * @param object $model * @param boolean $interactive * @return array $validate * @access public */ function doValidation(&$model, $interactive = true) { if (!is_object($model)) { return false; } $fields = $model->schema(); if (empty($fields)) { return false; } $validate = array(); $options = array('VALID_NOT_EMPTY', 'VALID_EMAIL', 'VALID_NUMER', 'VALID_YEAR'); if (class_exists('Validation')) { $parent = get_class_methods(get_parent_class('Validation')); $options = array_diff(get_class_methods('Validation'), $parent); } foreach ($fields as $fieldName => $field) { $prompt = 'Field: ' . $fieldName . "\n"; $prompt .= 'Type: ' . $field['type'] . "\n"; $prompt .= '---------------------------------------------------------------'."\n"; $prompt .= 'Please select one of the following validation options:'."\n"; $prompt .= '---------------------------------------------------------------'."\n"; $choices = array(); $skip = 1; sort($options); foreach ($options as $key => $option) { if ($option{0} != '_' && strtolower($option) != 'getinstance') { $prompt .= "{$skip} - {$option}\n"; $choices[$skip] = strtolower($option); $skip++; } } $methods = array_flip($choices); $prompt .= "{$skip} - Do not do any validation on this field.\n"; $prompt .= "... or enter in a valid regex validation string.\n"; $guess = $skip; if ($field['null'] != 1 && $fieldName != $model->primaryKey && !in_array($fieldName, array('created', 'modified', 'updated'))) { if ($fieldName == 'email') { $guess = $methods['email']; } elseif ($field['type'] == 'string') { $guess = $methods['alphanumeric']; } elseif ($field['type'] == 'integer') { $guess = $methods['numeric']; } elseif ($field['type'] == 'boolean') { $guess = $methods['numeric']; } elseif ($field['type'] == 'datetime') { $guess = $methods['date']; } } if ($interactive === true) { $this->out(''); $choice = $this->in($prompt, null, $guess); } else { $choice = $guess; } if ($choice != $skip) { if (is_numeric($choice) && isset($choices[$choice])) { $validate[$fieldName] = $choices[$choice]; } else { $validate[$fieldName] = $choice; } } } return $validate; }/** * Handles associations * * @param object $model * @param boolean $interactive * @return array $assocaitons * @access public */ function doAssociations(&$model, $interactive = true) { if (!is_object($model)) { return false; } $this->out(__('One moment while the associations are detected.', true)); $fields = $model->schema(); if (empty($fields)) { return false; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -