diff --git a/framework/yii/gii/generators/crud/Generator.php b/framework/yii/gii/generators/crud/Generator.php index 3ed51c1..7f1bff0 100644 --- a/framework/yii/gii/generators/crud/Generator.php +++ b/framework/yii/gii/generators/crud/Generator.php @@ -151,7 +151,7 @@ class Generator extends \yii\gii\Generator $templatePath = $this->getTemplatePath() . '/views'; foreach (scandir($templatePath) as $file) { - if (!in_array($file, array('create.php', 'update.php', 'view.php'))) { + if (!in_array($file, array('index.php', 'create.php', 'update.php', 'view.php', '_form.php'))) { continue; } if (is_file($templatePath . '/' . $file) && pathinfo($file, PATHINFO_EXTENSION) === 'php') { @@ -205,4 +205,55 @@ class Generator extends \yii\gii\Generator $pk = $class::primaryKey(); return $pk[0]; } + + /** + * @param ActiveRecord $model + * @param string $attribute + * @return string + */ + public function generateActiveField($model, $attribute) + { + $tableSchema = $model->getTableSchema(); + if (!isset($tableSchema->columns[$attribute])) { + return "\$form->field(\$model, '$attribute');"; + } + $column = $tableSchema->columns[$attribute]; + if ($column->phpType === 'boolean') { + return "\$form->field(\$model, '$attribute')->checkbox();"; + } elseif ($column->type === 'text') { + return "\$form->field(\$model, '$attribute')->textarea(array('rows' => 6));"; + } else { + if (preg_match('/^(password|pass|passwd|passcode)$/i', $column->name)) { + $input = 'passwordInput'; + } else { + $input = 'textInput'; + } + if ($column->phpType !== 'string' || $column->size === null) { + return "\$form->field(\$model, '$attribute')->$input();"; + } else { + return "\$form->field(\$model, '$attribute')->$input(array('maxlength' => $column->size));"; + } + } + } + + /** + * @param \yii\db\ColumnSchema $column + * @return string + */ + public function generateColumnFormat($column) + { + if ($column->phpType === 'boolean') { + return 'boolean'; + } elseif ($column->type === 'text') { + return 'ntext'; + } elseif (stripos($column->name, 'time') !== false && $column->phpType === 'integer') { + return 'datetime'; + } elseif (stripos($column->name, 'email') !== false) { + return 'email'; + } elseif (stripos($column->name, 'url') !== false) { + return 'url'; + } else { + return 'text'; + } + } } diff --git a/framework/yii/gii/generators/crud/templates/controller.php b/framework/yii/gii/generators/crud/templates/controller.php index 7b3cce8..da915c8 100644 --- a/framework/yii/gii/generators/crud/templates/controller.php +++ b/framework/yii/gii/generators/crud/templates/controller.php @@ -51,6 +51,20 @@ use yii\web\HttpException; class extends baseControllerClass) . "\n"; ?> { /** + * Lists all models. + * @return mixed + */ + public function actionIndex() + { + $dataProvider = new ActiveDataProvider(array( + 'query' => ::find(), + )); + return $this->render('index', array( + 'dataProvider' => $dataProvider, + )); + } + + /** * Displays a single model. * * @return mixed @@ -112,20 +126,8 @@ class extends '); - return $this->render('index', array( - 'dataProvider' => $dataProvider, - )); - } - - /** - * Returns the data model based on its primary key value. - * If the data model is not found, a 404 HTTP exception will be thrown. + * Finds the model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. * * @return the loaded model * @throws HttpException if the model cannot be found @@ -143,10 +145,10 @@ if (count($pks) === 1) { $condition = 'array(' . implode(', ', $condition) . ')'; } ?> - $model = ::find(); - if ($model === null) { + if (($model = ::find()) !== null) { + return $model; + } else { throw new HttpException(404, 'The requested page does not exist.'); } - return $model; } } diff --git a/framework/yii/gii/generators/crud/templates/views/_form.php b/framework/yii/gii/generators/crud/templates/views/_form.php index caf6f2a..6160e72 100644 --- a/framework/yii/gii/generators/crud/templates/views/_form.php +++ b/framework/yii/gii/generators/crud/templates/views/_form.php @@ -1,49 +1,45 @@ - -/* @var $this getControllerClass(); ?> */ -/* @var $model getModelClass(); ?> */ -/* @var $form CActiveForm */ -?> -
Fields with * are required.
+echo " - errorSummary(\$model); ?>\n"; ?> +use yii\helpers\Html; +use yii\widgets\ActiveForm; -tableSchema->columns as $column) -{ - if($column->autoIncrement) - continue; +/** + * @var yii\base\View $this + * @var modelClass, '\\'); ?> $model + * @var yii\widgets\ActiveForm $form + */ ?> -