Browse Source

CRUD WIP

tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
e683bd3ddb
  1. 53
      framework/yii/gii/generators/crud/Generator.php
  2. 36
      framework/yii/gii/generators/crud/templates/controller.php
  3. 72
      framework/yii/gii/generators/crud/templates/views/_form.php
  4. 65
      framework/yii/gii/generators/crud/templates/views/index.php
  5. 8
      framework/yii/gii/generators/crud/templates/views/update.php
  6. 23
      framework/yii/gii/generators/crud/templates/views/view.php

53
framework/yii/gii/generators/crud/Generator.php

@ -151,7 +151,7 @@ class Generator extends \yii\gii\Generator
$templatePath = $this->getTemplatePath() . '/views'; $templatePath = $this->getTemplatePath() . '/views';
foreach (scandir($templatePath) as $file) { 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; continue;
} }
if (is_file($templatePath . '/' . $file) && pathinfo($file, PATHINFO_EXTENSION) === 'php') { if (is_file($templatePath . '/' . $file) && pathinfo($file, PATHINFO_EXTENSION) === 'php') {
@ -205,4 +205,55 @@ class Generator extends \yii\gii\Generator
$pk = $class::primaryKey(); $pk = $class::primaryKey();
return $pk[0]; 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';
}
}
} }

36
framework/yii/gii/generators/crud/templates/controller.php

@ -51,6 +51,20 @@ use yii\web\HttpException;
class <?php echo $controllerClass; ?> extends <?php echo StringHelper::basename($generator->baseControllerClass) . "\n"; ?> class <?php echo $controllerClass; ?> extends <?php echo StringHelper::basename($generator->baseControllerClass) . "\n"; ?>
{ {
/** /**
* Lists all <?php echo $modelClass; ?> models.
* @return mixed
*/
public function actionIndex()
{
$dataProvider = new ActiveDataProvider(array(
'query' => <?php echo $modelClass; ?>::find(),
));
return $this->render('index', array(
'dataProvider' => $dataProvider,
));
}
/**
* Displays a single <?php echo $modelClass; ?> model. * Displays a single <?php echo $modelClass; ?> model.
* <?php echo $paramComments . "\n"; ?> * <?php echo $paramComments . "\n"; ?>
* @return mixed * @return mixed
@ -112,20 +126,8 @@ class <?php echo $controllerClass; ?> extends <?php echo StringHelper::basename(
} }
/** /**
* Lists all models. * Finds the <?php echo $modelClass; ?> model based on its primary key value.
* @return mixed * If the model is not found, a 404 HTTP exception will be thrown.
*/
public function actionIndex()
{
$dataProvider = new ActiveDataProvider('<?php echo $modelClass; ?>');
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.
* <?php echo $paramComments . "\n"; ?> * <?php echo $paramComments . "\n"; ?>
* @return <?php echo $modelClass; ?> the loaded model * @return <?php echo $modelClass; ?> the loaded model
* @throws HttpException if the model cannot be found * @throws HttpException if the model cannot be found
@ -143,10 +145,10 @@ if (count($pks) === 1) {
$condition = 'array(' . implode(', ', $condition) . ')'; $condition = 'array(' . implode(', ', $condition) . ')';
} }
?> ?>
$model = <?php echo $modelClass; ?>::find(<?php echo $condition; ?>); if (($model = <?php echo $modelClass; ?>::find(<?php echo $condition; ?>)) !== null) {
if ($model === null) { return $model;
} else {
throw new HttpException(404, 'The requested page does not exist.'); throw new HttpException(404, 'The requested page does not exist.');
} }
return $model;
} }
} }

72
framework/yii/gii/generators/crud/templates/views/_form.php

@ -1,49 +1,45 @@
<?php <?php
use yii\helpers\Inflector;
use yii\helpers\StringHelper;
/** /**
* The following variables are available in this template: * @var yii\base\View $this
* - $this: the CrudCode object * @var yii\gii\generators\crud\Generator $generator
*/ */
?>
<?php echo "<?php\n"; ?>
/* @var $this <?php echo $this->getControllerClass(); ?> */
/* @var $model <?php echo $this->getModelClass(); ?> */
/* @var $form CActiveForm */
?>
<div class="form"> /** @var \yii\db\ActiveRecord $model */
$class = $generator->modelClass;
<?php echo "<?php \$form=\$this->beginWidget('CActiveForm', array( $model = new $class;
'id'=>'".$this->class2id($this->modelClass)."-form', $safeAttributes = $model->safeAttributes();
// Please note: When you enable ajax validation, make sure the corresponding if (empty($safeAttributes)) {
// controller action is handling ajax validation correctly. $safeAttributes = $model->getTableSchema()->columnNames;
// There is a call to performAjaxValidation() commented in generated controller code. }
// See class documentation of CActiveForm for details on this.
'enableAjaxValidation'=>false,
)); ?>\n"; ?>
<p class="note">Fields with <span class="required">*</span> are required.</p> echo "<?php\n";
?>
<?php echo "<?php echo \$form->errorSummary(\$model); ?>\n"; ?> use yii\helpers\Html;
use yii\widgets\ActiveForm;
<?php /**
foreach($this->tableSchema->columns as $column) * @var yii\base\View $this
{ * @var <?php echo ltrim($generator->modelClass, '\\'); ?> $model
if($column->autoIncrement) * @var yii\widgets\ActiveForm $form
continue; */
?> ?>
<div class="row">
<?php echo "<?php echo ".$this->generateActiveLabel($this->modelClass,$column)."; ?>\n"; ?>
<?php echo "<?php echo ".$this->generateActiveField($this->modelClass,$column)."; ?>\n"; ?>
<?php echo "<?php echo \$form->error(\$model,'{$column->name}'); ?>\n"; ?>
</div>
<?php <div class="<?php echo Inflector::camel2id(StringHelper::basename($generator->modelClass)); ?>-form">
}
?> <?php echo '<?php'; ?> $form = ActiveForm::begin(); ?>
<div class="row buttons">
<?php echo "<?php echo CHtml::submitButton(\$model->isNewRecord ? 'Create' : 'Save'); ?>\n"; ?> <?php foreach ($safeAttributes as $attribute) {
</div> echo "\t\t<?php echo " . $generator->generateActiveField($model, $attribute) . " ?>\n\n";
} ?>
<div class="form-group">
<?php echo '<?php'; ?> echo Html::submitButton($model->isNewRecord ? 'Create' : 'Update', array('class' => 'btn btn-primary')); ?>
</div>
<?php echo "<?php \$this->endWidget(); ?>\n"; ?> <?php echo '<?php'; ?> ActiveForm::end(); ?>
</div><!-- form --> </div>

65
framework/yii/gii/generators/crud/templates/views/index.php

@ -1,29 +1,54 @@
<?php <?php
use yii\helpers\Inflector;
use yii\helpers\StringHelper;
/** /**
* The following variables are available in this template: * @var yii\base\View $this
* - $this: the CrudCode object * @var yii\gii\generators\crud\Generator $generator
*/ */
?>
<?php echo "<?php\n"; ?>
/* @var $this <?php echo $this->getControllerClass(); ?> */
/* @var $dataProvider CActiveDataProvider */
<?php /** @var \yii\db\ActiveRecord $model */
$label=$this->pluralize($this->class2name($this->modelClass)); $class = $generator->modelClass;
echo "\$this->breadcrumbs=array( $pks = $class::primaryKey();
'$label', if (count($pks) === 1) {
);\n"; $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 "<?php\n";
?> ?>
$this->menu=array( use yii\helpers\Html;
array('label'=>'Create <?php echo $this->modelClass; ?>', 'url'=>array('create')), use <?php echo $generator->indexWidgetType === 'grid' ? 'yii\grid\GridView' : 'yii\widgets\ListView'; ?>;
array('label'=>'Manage <?php echo $this->modelClass; ?>', 'url'=>array('admin')),
); /**
* @var yii\base\View $this
* @var yii\data\ActiveDataProvider $dataProvider
*/
$this->title = '<?php echo Inflector::pluralize(Inflector::camel2words(StringHelper::basename($generator->modelClass))); ?>';
?> ?>
<div class="<?php echo Inflector::camel2id(StringHelper::basename($generator->modelClass)); ?>-index">
<h1><?php echo "<?php"; ?> echo Html::encode($this->title); ?></h1>
<?php if ($generator->indexWidgetType === 'grid'): ?>
<h1><?php echo $label; ?></h1> <?php else: ?>
<?php echo "\t<?php"; ?> echo ListView::widget(array(
'dataProvider' => $dataProvider,
'itemView' => function ($model, $key, $index, $widget) {
return Html::a(Html::encode($model-><?php echo $nameAttribute; ?>), <?php echo $viewUrl; ?>);
},
)); ?>
<?php endif; ?>
<?php echo "<?php"; ?> $this->widget('zii.widgets.CListView', array( </div>
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
)); ?>

8
framework/yii/gii/generators/crud/templates/views/update.php

@ -14,11 +14,11 @@ echo "<?php\n";
use yii\helpers\Html; use yii\helpers\Html;
/** /**
* @var yii\base\View $this * @var yii\base\View $this
* @var <?php echo ltrim($generator->modelClass, '\\'); ?> $model * @var <?php echo ltrim($generator->modelClass, '\\'); ?> $model
*/ */
$this->title = 'Modify <?php echo Inflector::camel2words(StringHelper::basename($generator->modelClass)); ?>: ' . $model-><?php echo $generator->getNameAttribute(); ?>; $this->title = 'Update <?php echo Inflector::camel2words(StringHelper::basename($generator->modelClass)); ?>: ' . $model-><?php echo $generator->getNameAttribute(); ?>;
?> ?>
<div class="<?php echo Inflector::camel2id(StringHelper::basename($generator->modelClass)); ?>-update"> <div class="<?php echo Inflector::camel2id(StringHelper::basename($generator->modelClass)); ?>-update">

23
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\gii\generators\crud\Generator $generator
*/ */
/** @var \yii\db\ActiveRecord $model */
$class = $generator->modelClass;
$model = new $class;
echo "<?php\n"; echo "<?php\n";
?> ?>
use yii\helpers\Html; use yii\helpers\Html;
use yii\widgets\DetailView;
/** /**
* @var yii\base\View $this * @var yii\base\View $this
* @var <?php echo ltrim($generator->modelClass, '\\'); ?> $model * @var <?php echo ltrim($generator->modelClass, '\\'); ?> $model
*/ */
$this->title = $model-><?php echo $generator->getNameAttribute(); ?>; $this->title = $model-><?php echo $generator->getNameAttribute(); ?>;
?> ?>
@ -24,4 +29,16 @@ $this->title = $model-><?php echo $generator->getNameAttribute(); ?>;
<h1><?php echo "<?php"; ?> echo Html::encode($this->title); ?></h1> <h1><?php echo "<?php"; ?> echo Html::encode($this->title); ?></h1>
<?php echo '<?php'; ?> echo DetailView::widget(array(
'model' => $model,
'attributes' => array(
<?php
foreach ($model->getTableSchema()->columns as $column) {
$format = $generator->generateColumnFormat($column);
echo "\t\t\t'" . $column->name . ($format === 'text' ? '' : ':' . $format) . "',\n";
}
?>
),
)); ?>
</div> </div>

Loading…
Cancel
Save