📄 model.php
字号:
$primaryKey = $model->primaryKey; $foreignKey = $this->_modelKey($model->name); $associations = $possibleKeys = array(); //Look for belongsTo $i = 0; foreach ($fields as $fieldName => $field) { $offset = strpos($fieldName, '_id'); if ($fieldName != $model->primaryKey && $offset !== false) { $tmpModelName = $this->_modelNameFromKey($fieldName); $associations['belongsTo'][$i]['alias'] = $tmpModelName; $associations['belongsTo'][$i]['className'] = $tmpModelName; $associations['belongsTo'][$i]['foreignKey'] = $fieldName; $i++; } } //Look for hasOne and hasMany and hasAndBelongsToMany $i = $j = 0; foreach ($this->__tables as $otherTable) { App::import('Model'); $tmpModelName = $this->_modelName($otherTable); $tempOtherModel = & new Model(array('name' => $tmpModelName, 'table' => $otherTable, 'ds' => $model->useDbConfig)); $modelFieldsTemp = $tempOtherModel->schema(); $offset = strpos($otherTable, $model->table . '_'); $otherOffset = strpos($otherTable, '_' . $model->table); foreach ($modelFieldsTemp as $fieldName => $field) { if ($field['type'] == 'integer' || $field['type'] == 'string') { $possibleKeys[$otherTable][] = $fieldName; } if ($fieldName != $model->primaryKey && $fieldName == $foreignKey && $offset === false && $otherOffset === false) { $associations['hasOne'][$j]['alias'] = $tempOtherModel->name; $associations['hasOne'][$j]['className'] = $tempOtherModel->name; $associations['hasOne'][$j]['foreignKey'] = $fieldName; $associations['hasMany'][$j]['alias'] = $tempOtherModel->name; $associations['hasMany'][$j]['className'] = $tempOtherModel->name; $associations['hasMany'][$j]['foreignKey'] = $fieldName; $j++; } } if ($offset !== false) { $offset = strlen($model->table . '_'); $tmpModelName = $this->_modelName(substr($otherTable, $offset)); $associations['hasAndBelongsToMany'][$i]['alias'] = $tmpModelName; $associations['hasAndBelongsToMany'][$i]['className'] = $tmpModelName; $associations['hasAndBelongsToMany'][$i]['foreignKey'] = $foreignKey; $associations['hasAndBelongsToMany'][$i]['associationForeignKey'] = $this->_modelKey($tmpModelName); $associations['hasAndBelongsToMany'][$i]['joinTable'] = $otherTable; $i++; } if ($otherOffset !== false) { $tmpModelName = $this->_modelName(substr($otherTable, 0, $otherOffset)); $associations['hasAndBelongsToMany'][$i]['alias'] = $tmpModelName; $associations['hasAndBelongsToMany'][$i]['className'] = $tmpModelName; $associations['hasAndBelongsToMany'][$i]['foreignKey'] = $foreignKey; $associations['hasAndBelongsToMany'][$i]['associationForeignKey'] = $this->_modelKey($tmpModelName); $associations['hasAndBelongsToMany'][$i]['joinTable'] = $otherTable; $i++; } } if ($interactive !== true) { unset($associations['hasOne']); } if ($interactive === true) { $this->hr(); if (empty($associations)) { $this->out(__('None found.', true)); } else { $this->out(__('Please confirm the following associations:', true)); $this->hr(); 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) || 'no' == low($response)) { unset($associations[$type][$i]); } else { if ($model->name === $associations[$type][$i]['alias']) { if ($type === 'belongsTo') { $alias = 'Parent' . $associations[$type][$i]['alias']; } if($type === 'hasOne' || $type === 'hasMany') { $alias = 'Child' . $associations[$type][$i]['alias']; } $alternateAlias = $this->in(sprintf(__('This is a self join. Use %s as the alias', true), $alias), array('y', 'n'), 'y'); if ('n' == low($alternateAlias) || 'no' == low($alternateAlias)) { $associations[$type][$i]['alias'] = $this->in(__('Specify an alternate alias.', true)); } else { $associations[$type][$i]['alias'] = $alias; } } } } $associations[$type] = array_merge($associations[$type]); } } } $wannaDoMoreAssoc = $this->in(__('Would you like to define some additional model associations?', true), array('y','n'), 'n'); while ((low($wannaDoMoreAssoc) == 'y' || low($wannaDoMoreAssoc) == 'yes')) { $assocs = array(1 => 'belongsTo', 2 => 'hasOne', 3 => 'hasMany', 4 => 'hasAndBelongsToMany'); $bad = true; while ($bad) { $this->out(__('What is the association type?', true)); $prompt = "1. belongsTo\n"; $prompt .= "2. hasOne\n"; $prompt .= "3. hasMany\n"; $prompt .= "4. hasAndBelongsToMany\n"; $assocType = intval($this->in($prompt, null, __("Enter a number", true))); if (intval($assocType) < 1 || intval($assocType) > 4) { $this->out(__('The selection you entered was invalid. Please enter a number between 1 and 4.', true)); } else { $bad = false; } } $this->out(__('For the following options be very careful to match your setup exactly. Any 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; if ($assocType == '1') { $showKeys = $possibleKeys[$model->table]; $suggestedForeignKey = $this->_modelKey($alias); } else { $otherTable = Inflector::tableize($className); if (in_array($otherTable, $this->__tables)) { if ($assocType < '4') { $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)); for ($i = 0; $i < count($showKeys); $i++) { $this->out($i + 1 . ". " . $showKeys[$i]); } $foreignKey = $this->in(__('What is the foreignKey?', true), null, __("Enter a number", true)); if (intval($foreignKey) > 0 && intval($foreignKey) <= $i ) { $foreignKey = $showKeys[intval($foreignKey) - 1]; } } if (!isset($foreignKey)) { $foreignKey = $this->in(__('What is the foreignKey? Specify your own.', true), null, $suggestedForeignKey); } if ($assocType == '4') { $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; if ($assocType == '4') { $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; }/** * 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); $validate = $this->doValidation($name, $associations); } $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++) { $out .= "\t\t'" . $keys[$i] . "' => array('" . $validate[$keys[$i]] . "')"; if ($i + 1 < $validateCount) { $out .= ","; } $out .= "\n"; } $out .= "\t);\n"; } $out .= "\n"; if (!empty($associations)) { if (!empty($associations['belongsTo']) || !empty($associations['$hasOne']) || !empty($associations['hasMany']) || !empty($associations['hasAndBelongsToMany'])) { $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++) { $out .= "\t\t\t'{$associations['belongsTo'][$i]['alias']}' => "; $out .= "array('className' => '{$associations['belongsTo'][$i]['className']}',\n"; $out .= "\t\t\t\t\t\t\t\t'foreignKey' => '{$associations['belongsTo'][$i]['foreignKey']}',\n"; $out .= "\t\t\t\t\t\t\t\t'conditions' => '',\n"; $out .= "\t\t\t\t\t\t\t\t'fields' => '',\n"; $out .= "\t\t\t\t\t\t\t\t'order' => ''\n"; $out .= "\t\t\t)"; 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++) { $out .= "\t\t\t'{$associations['hasOne'][$i]['alias']}' => "; $out .= "array('className' => '{$associations['hasOne'][$i]['className']}',\n"; $out .= "\t\t\t\t\t\t\t\t'foreignKey' => '{$associations['hasOne'][$i]['foreignKey']}',\n"; $out .= "\t\t\t\t\t\t\t\t'dependent' => false,\n"; $out .= "\t\t\t\t\t\t\t\t'conditions' => '',\n"; $out .= "\t\t\t\t\t\t\t\t'fields' => '',\n"; $out .= "\t\t\t\t\t\t\t\t'order' => ''\n"; $out .= "\t\t\t)"; 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++) { $out .= "\t\t\t'{$associations['hasMany'][$i]['alias']}' => "; $out .= "array('className' => '{$associations['hasMany'][$i]['className']}',\n"; $out .= "\t\t\t\t\t\t\t\t'foreignKey' => '{$associations['hasMany'][$i]['foreignKey']}',\n"; $out .= "\t\t\t\t\t\t\t\t'dependent' => false,\n"; $out .= "\t\t\t\t\t\t\t\t'conditions' => '',\n"; $out .= "\t\t\t\t\t\t\t\t'fields' => '',\n"; $out .= "\t\t\t\t\t\t\t\t'order' => '',\n"; $out .= "\t\t\t\t\t\t\t\t'limit' => '',\n"; $out .= "\t\t\t\t\t\t\t\t'offset' => '',\n"; $out .= "\t\t\t\t\t\t\t\t'exclusive' => '',\n"; $out .= "\t\t\t\t\t\t\t\t'finderQuery' => '',\n"; $out .= "\t\t\t\t\t\t\t\t'counterQuery' => ''\n"; $out .= "\t\t\t)"; if ($i + 1 < $hasManyCount) { $out .= ","; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -