📄 bake.php.svn-base
字号:
if(intval($assocType) < 1 || intval($assocType) > 4) { $this->stdout('The selection you entered was invalid. Please enter a number between 1 and 4.'); } else { $bad = false; } } $this->stdout('For the following options be very careful to match your setup exactly. Any spelling mistakes will cause errors.'); $this->hr(); $associationName = $this->getInput('What is the name of this association?'); $className = $this->getInput('What className will '.$associationName.' use?', null, $associationName ); $suggestedForeignKey = null; if($assocType == '1') { $showKeys = $possibleKeys[$useTable]; $suggestedForeignKey = $this->__modelKey($associationName); } else { $otherTable = Inflector::tableize($className); if(in_array($otherTable, $this->__tables)) { if($assocType < '4') { $showKeys = $possibleKeys[$otherTable]; } else { $showKeys = null; } } else { $otherTable = $this->getInput('What is the table for this class?'); $showKeys = $possibleKeys[$otherTable]; } $suggestedForeignKey = $this->__modelKey($currentModelName); } if(!empty($showKeys)) { $this->stdout('A helpful List of possible keys'); for ($i = 0; $i < count($showKeys); $i++) { $this->stdout($i + 1 . ". " . $showKeys[$i]); } $foreignKey = $this->getInput('What is the foreignKey? Choose a number.'); if (intval($foreignKey) > 0 && intval($foreignKey) <= $i ) { $foreignKey = $showKeys[intval($foreignKey) - 1]; } } if(!isset($foreignKey)) { $foreignKey = $this->getInput('What is the foreignKey? Specify your own.', null, $suggestedForeignKey); } if($assocType == '4') { $associationForeignKey = $this->getInput('What is the associationForeignKey?', null, $this->__modelKey($currentModelName)); $joinTable = $this->getInput('What is the joinTable?'); } $associations[$assocs[$assocType]] = array_values($associations[$assocs[$assocType]]); $count = count($associations[$assocs[$assocType]]); $i = ($count > 0) ? $count : 0; $associations[$assocs[$assocType]][$i]['alias'] = $associationName; $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->getInput('Define another association?', array('y','n'), 'y'); } } $this->stdout(''); $this->hr(); $this->stdout('The following model will be created:'); $this->hr(); $this->stdout("Model Name: $currentModelName"); $this->stdout("DB Connection: " . ($usingDefault ? 'default' : $useDbConfig)); $this->stdout("Model Table: " . $useTable); $this->stdout("Validation: " . print_r($validate, true)); if(!empty($associations)) { $this->stdout("Associations:"); if(count($associations['belongsTo'])) { for($i = 0; $i < count($associations['belongsTo']); $i++) { $this->stdout(" $currentModelName belongsTo {$associations['belongsTo'][$i]['alias']}"); } } if(count($associations['hasOne'])) { for($i = 0; $i < count($associations['hasOne']); $i++) { $this->stdout(" $currentModelName hasOne {$associations['hasOne'][$i]['alias']}"); } } if(count($associations['hasMany'])) { for($i = 0; $i < count($associations['hasMany']); $i++) { $this->stdout(" $currentModelName hasMany {$associations['hasMany'][$i]['alias']}"); } } if(count($associations['hasAndBelongsToMany'])) { for($i = 0; $i < count($associations['hasAndBelongsToMany']); $i++) { $this->stdout(" $currentModelName hasAndBelongsToMany {$associations['hasAndBelongsToMany'][$i]['alias']}"); } } } $this->hr(); $looksGood = $this->getInput('Look okay?', array('y','n'), 'y'); if (low($looksGood) == 'y' || low($looksGood) == 'yes') { if ($useTable == Inflector::tableize($currentModelName)) { // set it to null... // putting $useTable in the model // is unnecessary. $useTable = null; } $this->bakeModel($currentModelName, $useDbConfig, $useTable, $primaryKey, $validate, $associations); if ($this->doUnitTest()) { $this->bakeUnitTest('model', $currentModelName); } } else { $this->stdout('Bake Aborted.'); } }/** * Action to create a View. * */ function doView() { $this->hr(); $this->stdout('View Bake:'); $this->hr(); $uses = array(); $wannaUseSession = 'y'; $wannaDoScaffold = 'y'; $useDbConfig = 'default'; $this->__doList($useDbConfig, 'Controllers'); $enteredController = ''; while ($enteredController == '') { $enteredController = $this->getInput('Enter a number from the list above, or type in the name of another controller.'); if ($enteredController == '' || intval($enteredController) > count($this->__controllerNames)) { $this->stdout('Error:'); $this->stdout("The Controller name you supplied was empty, or the number \nyou selected was not an option. Please try again."); $enteredController = ''; } } if (intval($enteredController) > 0 && intval($enteredController) <= count($this->__controllerNames) ) { $controllerName = $this->__controllerNames[intval($enteredController) - 1]; } else { $controllerName = $enteredController; } $controllerPath = $this->__controllerPath($controllerName); $doItInteractive = $this->getInput("Would you like bake to build your views interactively?\nWarning: Choosing no will overwrite {$controllerClassName} views if it exist.", array('y','n'), 'y'); if (low($doItInteractive) == 'y' || low($doItInteractive) == 'yes') { $this->interactive = true; $wannaDoScaffold = $this->getInput("Would you like to create some scaffolded views (index, add, view, edit) for this controller?\nNOTE: Before doing so, you'll need to create your controller and model classes (including associated models).", array('y','n'), 'n'); } $admin = null; $admin_url = null; if (low($wannaDoScaffold) == 'y' || low($wannaDoScaffold) == 'yes') { $wannaDoAdmin = $this->getInput("Would you like to create the views for admin routing?", array('y','n'), 'n'); } if ((low($wannaDoAdmin) == 'y' || low($wannaDoAdmin) == 'yes')) { require(CONFIGS.'core.php'); if(defined('CAKE_ADMIN')) { $admin = CAKE_ADMIN . '_'; $admin_url = '/'.CAKE_ADMIN; } else { $adminRoute = ''; $this->stdout('You need to enable CAKE_ADMIN in /app/config/core.php to use admin routing.'); $this->stdout('What would you like the admin route to be?'); $this->stdout('Example: www.example.com/admin/controller'); while ($adminRoute == '') { $adminRoute = $this->getInput("What would you like the admin route to be?", null, 'admin'); } if($this->__addAdminRoute($adminRoute) !== true){ $this->stdout('Unable to write to /app/config/core.php.'); $this->stdout('You need to enable CAKE_ADMIN in /app/config/core.php to use admin routing.'); exit(); } else { $admin = $adminRoute . '_'; $admin_url = '/'.$adminRoute; } } } if (low($wannaDoScaffold) == 'y' || low($wannaDoScaffold) == 'yes') { $file = CONTROLLERS . $controllerPath . '_controller.php'; if(!file_exists($file)) { $shortPath = str_replace(ROOT, null, $file); $shortPath = str_replace('../', '', $shortPath); $shortPath = str_replace('//', '/', $shortPath); $this->stdout(''); $this->stdout("The file '$shortPath' could not be found.\nIn order to scaffold, you'll need to first create the controller. "); $this->stdout(''); die(); } else { loadController($controllerName); loadModels(); if($admin) { $this->__bakeViews($controllerName, $controllerPath, $admin, $admin_url); } $this->__bakeViews($controllerName, $controllerPath, null, null); $this->hr(); $this->stdout(''); $this->stdout('View Scaffolding Complete.'."\n"); } } else { $actionName = ''; while ($actionName == '') { $actionName = $this->getInput('Action Name? (use camelCased function name)'); if ($actionName == '') { $this->stdout('The action name you supplied was empty. Please try again.'); } } $this->stdout(''); $this->hr(); $this->stdout('The following view will be created:'); $this->hr(); $this->stdout("Controller Name: $controllerName"); $this->stdout("Action Name: $actionName"); $this->stdout("Path: app/views/" . $controllerPath . DS . Inflector::underscore($actionName) . '.thtml'); $this->hr(); $looksGood = $this->getInput('Look okay?', array('y','n'), 'y'); if (low($looksGood) == 'y' || low($looksGood) == 'yes') { $this->bakeView($controllerName, $actionName); } else { $this->stdout('Bake Aborted.'); } } } function __bakeViews($controllerName, $controllerPath, $admin= null, $admin_url = null) { $controllerClassName = $controllerName.'Controller'; $controllerObj = & new $controllerClassName(); if(!in_array('Html', $controllerObj->helpers)) { $controllerObj->helpers[] = 'Html'; } if(!in_array('Form', $controllerObj->helpers)) { $controllerObj->helpers[] = 'Form'; } $controllerObj->constructClasses(); $currentModelName = $controllerObj->modelClass; $this->__modelClass = $currentModelName; $modelKey = Inflector::underscore($currentModelName); $modelObj =& ClassRegistry::getObject($modelKey); $singularName = $this->__singularName($currentModelName); $pluralName = $this->__pluralName($currentModelName); $singularHumanName = $this->__singularHumanName($currentModelName); $pluralHumanName = $this->__pluralHumanName($controllerName); $fieldNames = $controllerObj->generateFieldNames(null, false); //-------------------------[INDEX]-------------------------// $indexView = null; if(!empty($modelObj->alias)) { foreach ($modelObj->alias as $key => $value) { $alias[] = $key; } } $indexView .= "<div class=\"{$pluralName}\">\n"; $indexView .= "<h2>List " . $pluralHumanName . "</h2>\n\n"; $indexView .= "<table cellpadding=\"0\" cellspacing=\"0\">\n"; $indexView .= "<tr>\n"; foreach ($fieldNames as $fieldName) { $indexView .= "\t<th>".$fieldName['prompt']."</th>\n"; } $indexView .= "\t<th>Actions</th>\n"; $indexView .= "</tr>\n"; $indexView .= "<?php foreach (\${$pluralName} as \${$singularName}): ?>\n"; $indexView .= "<tr>\n"; $count = 0; foreach($fieldNames as $field => $value) { if(isset($value['foreignKey'])) { $otherModelName = $this->__modelName($value['model']); $otherModelKey = Inflector::underscore($otherModelName); $otherModelObj =& ClassRegistry::getObject($otherModelKey); $otherControllerName = $this->__controllerName($otherModelName); $otherControllerPath = $this->__controllerPath($otherControllerName); if(is_object($otherModelObj)) { $displayField = $otherModelObj->getDisplayField(); $indexView .= "\t<td> <?php echo \$html->link(\$".$singularName."['{$alias[$count]}']['{$displayField}'], '{$admin_url}/" . $otherControllerPath . "/view/' .\$".$singularName."['{$alias[$count]}']['{$otherModelObj->primaryKey}'])?></td>\n"; } else { $indexView .= "\t<td><?php echo \$".$singularName."['{$modelObj->name}']['{$field}']; ?></td>\n"; } $count++; } else { $indexView .= "\t<td><?php echo \$".$singularName."['{$modelObj->name}']['{$field}']; ?></td>\n"; } } $indexView .= "\t<td nowrap>\n"; $indexView .= "\t\t<?php echo \$html->link('View','{$admin_url}/{$controllerPath}/view/' . \$".$singularName."['{$modelObj->name}']['{$modelObj->primaryKey}'])?>\n"; $indexView .= "\t\t<?php echo \$html->link('Edit','{$admin_url}/{$controllerPath}/edit/' . \$".$singularName."['{$modelObj->name}']['{$modelObj->primaryKey}'])?>\n"; $indexView .= "\t\t<?php echo \$html->link('Delete','{$admin_url}/{$controllerPath}/delete/' . \$".$singularName."['{$modelObj->name}']['{$modelObj->primaryKey}'], null, 'Are you sure you want to delete id ' . \$".$singularName."['{$modelObj->name}']['{$modelObj->primaryKey}'])?>\n"; $indexView .= "\t</td>\n"; $indexView .= "</tr>\n"; $indexView .= "<?php endforeach; ?>\n"; $indexView .= "</table>\n\n"; $indexView .= "<ul class=\"actions\">\n"; $indexView .= "\t<li><?php echo \$html->link('New {$singularHumanName}', '{$admin_url}/{$controllerPath}/add'); ?></li>\n"; $indexView .= "</ul>\n"; $indexView .= "</div>"; //-------------------------[VIEW]-------------------------// $viewView = null; $viewView .= "<div class=\"{$singularName}\">\n"; $viewView .= "<h2>View " . $singularHumanName . "</h2>\n\n"; $viewView .= "<dl>\n"; $count = 0; foreach($fieldNames as $field => $value) { $viewView .= "\t<dt>" . $value['prompt'] . "</dt>\n"; if(isset($value['foreignKey'])) { $otherModelName = $this->__modelName($value['model']);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -