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 */ -?> -
- -beginWidget('CActiveForm', array( - 'id'=>'".$this->class2id($this->modelClass)."-form', - // Please note: When you enable ajax validation, make sure the corresponding - // controller action is handling ajax validation correctly. - // There is a call to performAjaxValidation() commented in generated controller code. - // See class documentation of CActiveForm for details on this. - 'enableAjaxValidation'=>false, -)); ?>\n"; ?> +/** @var \yii\db\ActiveRecord $model */ +$class = $generator->modelClass; +$model = new $class; +$safeAttributes = $model->safeAttributes(); +if (empty($safeAttributes)) { + $safeAttributes = $model->getTableSchema()->columnNames; +} -

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 + */ ?> -
- generateActiveLabel($this->modelClass,$column)."; ?>\n"; ?> - generateActiveField($this->modelClass,$column)."; ?>\n"; ?> - error(\$model,'{$column->name}'); ?>\n"; ?> -
- -
- isNewRecord ? 'Create' : 'Save'); ?>\n"; ?> -
+
+ + $form = ActiveForm::begin(); ?> + +generateActiveField($model, $attribute) . " ?>\n\n"; +} ?> +
+ echo Html::submitButton($model->isNewRecord ? 'Create' : 'Update', array('class' => 'btn btn-primary')); ?> +
-endWidget(); ?>\n"; ?> + ActiveForm::end(); ?> -
\ No newline at end of file +
diff --git a/framework/yii/gii/generators/crud/templates/views/index.php b/framework/yii/gii/generators/crud/templates/views/index.php index a115251..fbac7c3 100644 --- a/framework/yii/gii/generators/crud/templates/views/index.php +++ b/framework/yii/gii/generators/crud/templates/views/index.php @@ -1,29 +1,54 @@ - -/* @var $this getControllerClass(); ?> */ -/* @var $dataProvider CActiveDataProvider */ -pluralize($this->class2name($this->modelClass)); -echo "\$this->breadcrumbs=array( - '$label', -);\n"; +/** @var \yii\db\ActiveRecord $model */ +$class = $generator->modelClass; +$pks = $class::primaryKey(); +if (count($pks) === 1) { + $viewUrl = "array('view', 'id' => \$model->{$pks[0]})"; +} else { + $params = array(); + foreach ($pks as $pk) { + $params[] = "'$pk' => \$model->$pk"; + } + $viewUrl = "array('view', " . implode(', ', $params) . ')'; +} + +$nameAttribute = $generator->getNameAttribute(); + +echo " -$this->menu=array( - array('label'=>'Create modelClass; ?>', 'url'=>array('create')), - array('label'=>'Manage modelClass; ?>', 'url'=>array('admin')), -); +use yii\helpers\Html; +use indexWidgetType === 'grid' ? 'yii\grid\GridView' : 'yii\widgets\ListView'; ?>; + +/** + * @var yii\base\View $this + * @var yii\data\ActiveDataProvider $dataProvider + */ + +$this->title = 'modelClass))); ?>'; ?> +
+ +

echo Html::encode($this->title); ?>

+ +indexWidgetType === 'grid'): ?> -

+ + echo ListView::widget(array( + 'dataProvider' => $dataProvider, + 'itemView' => function ($model, $key, $index, $widget) { + return Html::a(Html::encode($model->), ); + }, + )); ?> + - $this->widget('zii.widgets.CListView', array( - 'dataProvider'=>$dataProvider, - 'itemView'=>'_view', -)); ?> +
diff --git a/framework/yii/gii/generators/crud/templates/views/update.php b/framework/yii/gii/generators/crud/templates/views/update.php index 8dc3c73..bacc1ec 100644 --- a/framework/yii/gii/generators/crud/templates/views/update.php +++ b/framework/yii/gii/generators/crud/templates/views/update.php @@ -14,11 +14,11 @@ echo "modelClass, '\\'); ?> $model -*/ + * @var yii\base\View $this + * @var modelClass, '\\'); ?> $model + */ -$this->title = 'Modify modelClass)); ?>: ' . $model->getNameAttribute(); ?>; +$this->title = 'Update modelClass)); ?>: ' . $model->getNameAttribute(); ?>; ?>
diff --git a/framework/yii/gii/generators/crud/templates/views/view.php b/framework/yii/gii/generators/crud/templates/views/view.php index baca46f..214f10b 100644 --- a/framework/yii/gii/generators/crud/templates/views/view.php +++ b/framework/yii/gii/generators/crud/templates/views/view.php @@ -8,15 +8,20 @@ use yii\helpers\StringHelper; * @var yii\gii\generators\crud\Generator $generator */ +/** @var \yii\db\ActiveRecord $model */ +$class = $generator->modelClass; +$model = new $class; + echo " use yii\helpers\Html; +use yii\widgets\DetailView; /** -* @var yii\base\View $this -* @var modelClass, '\\'); ?> $model -*/ + * @var yii\base\View $this + * @var modelClass, '\\'); ?> $model + */ $this->title = $model->getNameAttribute(); ?>; ?> @@ -24,4 +29,16 @@ $this->title = $model->getNameAttribute(); ?>;

echo Html::encode($this->title); ?>

+ echo DetailView::widget(array( + 'model' => $model, + 'attributes' => array( +getTableSchema()->columns as $column) { + $format = $generator->generateColumnFormat($column); + echo "\t\t\t'" . $column->name . ($format === 'text' ? '' : ':' . $format) . "',\n"; +} +?> + ), + )); ?> +