Commit 4cbdd7a6 by Qiang Xue

crud WIP

parent db212f05
...@@ -81,11 +81,6 @@ yii.gii = (function ($) { ...@@ -81,11 +81,6 @@ yii.gii = (function ($) {
$('#model-generator .field-generator-modelclass').toggle($(this).val().indexOf('*') == -1); $('#model-generator .field-generator-modelclass').toggle($(this).val().indexOf('*') == -1);
}).change(); }).change();
// crud generator: hide Search Model Class input if search is not enabled
$('#crud-generator #generator-enablesearch').on('change', function () {
$('#crud-generator .field-generator-searchmodelclass').toggle(this.checked);
}).change();
// hide Generate button if any input is changed // hide Generate button if any input is changed
$('.default-view .form-group input,select,textarea').change(function () { $('.default-view .form-group input,select,textarea').change(function () {
$('.default-view-results,.default-view-files').hide(); $('.default-view-results,.default-view-files').hide();
......
...@@ -10,6 +10,7 @@ namespace yii\gii\generators\crud; ...@@ -10,6 +10,7 @@ namespace yii\gii\generators\crud;
use Yii; use Yii;
use yii\base\Model; use yii\base\Model;
use yii\db\ActiveRecord; use yii\db\ActiveRecord;
use yii\db\Schema;
use yii\gii\CodeFile; use yii\gii\CodeFile;
use yii\helpers\Inflector; use yii\helpers\Inflector;
use yii\web\Controller; use yii\web\Controller;
...@@ -26,7 +27,6 @@ class Generator extends \yii\gii\Generator ...@@ -26,7 +27,6 @@ class Generator extends \yii\gii\Generator
public $controllerClass; public $controllerClass;
public $baseControllerClass = 'yii\web\Controller'; public $baseControllerClass = 'yii\web\Controller';
public $indexWidgetType = 'grid'; public $indexWidgetType = 'grid';
public $enableSearch = true;
public $searchModelClass; public $searchModelClass;
public function getName() public function getName()
...@@ -44,16 +44,14 @@ class Generator extends \yii\gii\Generator ...@@ -44,16 +44,14 @@ class Generator extends \yii\gii\Generator
{ {
return array_merge(parent::rules(), array( return array_merge(parent::rules(), array(
array('moduleID, controllerClass, modelClass, searchModelClass, baseControllerClass', 'filter', 'filter' => 'trim'), array('moduleID, controllerClass, modelClass, searchModelClass, baseControllerClass', 'filter', 'filter' => 'trim'),
array('modelClass, controllerClass, baseControllerClass, indexWidgetType', 'required'), array('modelClass, searchModelClass, controllerClass, baseControllerClass, indexWidgetType', 'required'),
array('modelClass, controllerClass, baseControllerClass, searchModelClass', 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'), array('modelClass, controllerClass, baseControllerClass, searchModelClass', 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'),
array('modelClass', 'validateClass', 'params' => array('extends' => ActiveRecord::className())), array('modelClass', 'validateClass', 'params' => array('extends' => ActiveRecord::className())),
array('baseControllerClass', 'validateClass', 'params' => array('extends' => Controller::className())), array('baseControllerClass', 'validateClass', 'params' => array('extends' => Controller::className())),
array('controllerClass', 'match', 'pattern' => '/Controller$/', 'message' => 'Controller class name must be suffixed with "Controller".'), array('controllerClass', 'match', 'pattern' => '/Controller$/', 'message' => 'Controller class name must be suffixed with "Controller".'),
array('controllerClass, searchModelClass', 'validateNewClass'), array('controllerClass, searchModelClass', 'validateNewClass'),
array('enableSearch', 'boolean'),
array('indexWidgetType', 'in', 'range' => array('grid', 'list')), array('indexWidgetType', 'in', 'range' => array('grid', 'list')),
array('modelClass', 'validateModelClass'), array('modelClass', 'validateModelClass'),
array('searchModelClass', 'validateSearchModelClass'),
array('moduleID', 'validateModuleID'), array('moduleID', 'validateModuleID'),
)); ));
} }
...@@ -66,7 +64,6 @@ class Generator extends \yii\gii\Generator ...@@ -66,7 +64,6 @@ class Generator extends \yii\gii\Generator
'controllerClass' => 'Controller Class', 'controllerClass' => 'Controller Class',
'baseControllerClass' => 'Base Controller Class', 'baseControllerClass' => 'Base Controller Class',
'indexWidgetType' => 'Widget Used in Index Page', 'indexWidgetType' => 'Widget Used in Index Page',
'enableSearch' => 'Enable Search',
'searchModelClass' => 'Search Model Class', 'searchModelClass' => 'Search Model Class',
)); ));
} }
...@@ -87,11 +84,8 @@ class Generator extends \yii\gii\Generator ...@@ -87,11 +84,8 @@ class Generator extends \yii\gii\Generator
If not set, it means the controller will belong to the application.', If not set, it means the controller will belong to the application.',
'indexWidgetType' => 'This is the widget type to be used in the index page to display list of the models. 'indexWidgetType' => 'This is the widget type to be used in the index page to display list of the models.
You may choose either <code>GridView</code> or <code>ListView</code>', You may choose either <code>GridView</code> or <code>ListView</code>',
'enableSearch' => 'Whether to enable the search functionality on the index page. When search is enabled,
a search form will be displayed on the index page, and the index page will display the search results.',
'searchModelClass' => 'This is the class representing the data being collecting in the search form. 'searchModelClass' => 'This is the class representing the data being collecting in the search form.
A fully qualified namespaced class name is required, e.g., <code>app\models\PostSearchForm</code>. A fully qualified namespaced class name is required, e.g., <code>app\models\search\PostSearch</code>.',
This is only used when search is enabled.',
); );
} }
...@@ -107,7 +101,7 @@ class Generator extends \yii\gii\Generator ...@@ -107,7 +101,7 @@ class Generator extends \yii\gii\Generator
*/ */
public function stickyAttributes() public function stickyAttributes()
{ {
return array('baseControllerClass', 'moduleID', 'indexWidgetType', 'enableSearch'); return array('baseControllerClass', 'moduleID', 'indexWidgetType');
} }
public function validateModelClass() public function validateModelClass()
...@@ -120,13 +114,6 @@ class Generator extends \yii\gii\Generator ...@@ -120,13 +114,6 @@ class Generator extends \yii\gii\Generator
} }
} }
public function validateSearchModelClass()
{
if ($this->enableSearch && empty($this->searchModelClass)) {
$this->addError('searchModelClass', 'Search Model Class cannot be empty.');
}
}
public function validateModuleID() public function validateModuleID()
{ {
if (!empty($this->moduleID)) { if (!empty($this->moduleID)) {
...@@ -142,26 +129,21 @@ class Generator extends \yii\gii\Generator ...@@ -142,26 +129,21 @@ class Generator extends \yii\gii\Generator
*/ */
public function generate() public function generate()
{ {
$files = array(); $controllerFile = Yii::getAlias('@' . str_replace('\\', '/', ltrim($this->controllerClass, '\\')) . '.php');
$files[] = new CodeFile( $searchModel = Yii::getAlias('@' . str_replace('\\', '/', ltrim($this->searchModelClass, '\\') . '.php'));
$this->getControllerFile(), $files = array(
$this->render('controller.php') new CodeFile($controllerFile, $this->render('controller.php')),
new CodeFile($searchModel, $this->render('search.php')),
); );
$viewPath = $this->getViewPath();
$viewPath = $this->getViewPath();
$templatePath = $this->getTemplatePath() . '/views'; $templatePath = $this->getTemplatePath() . '/views';
foreach (scandir($templatePath) as $file) { foreach (scandir($templatePath) as $file) {
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') { if (is_file($templatePath . '/' . $file) && pathinfo($file, PATHINFO_EXTENSION) === 'php') {
$files[] = new CodeFile("$viewPath/$file", $this->render("views/$file")); $files[] = new CodeFile("$viewPath/$file", $this->render("views/$file"));
} }
} }
if ($this->enableSearch) {
}
return $files; return $files;
} }
...@@ -177,14 +159,6 @@ class Generator extends \yii\gii\Generator ...@@ -177,14 +159,6 @@ class Generator extends \yii\gii\Generator
} }
/** /**
* @return string the controller class file path
*/
public function getControllerFile()
{
return Yii::getAlias('@' . str_replace('\\', '/', ltrim($this->controllerClass, '\\')) . '.php');
}
/**
* @return string the action view file path * @return string the action view file path
*/ */
public function getViewPath() public function getViewPath()
...@@ -207,13 +181,12 @@ class Generator extends \yii\gii\Generator ...@@ -207,13 +181,12 @@ class Generator extends \yii\gii\Generator
} }
/** /**
* @param ActiveRecord $model
* @param string $attribute * @param string $attribute
* @return string * @return string
*/ */
public function generateActiveField($model, $attribute) public function generateActiveField($attribute)
{ {
$tableSchema = $model->getTableSchema(); $tableSchema = $this->getTableSchema();
if (!isset($tableSchema->columns[$attribute])) { if (!isset($tableSchema->columns[$attribute])) {
return "\$form->field(\$model, '$attribute');"; return "\$form->field(\$model, '$attribute');";
} }
...@@ -237,6 +210,21 @@ class Generator extends \yii\gii\Generator ...@@ -237,6 +210,21 @@ class Generator extends \yii\gii\Generator
} }
/** /**
* @param string $attribute
* @return string
*/
public function generateActiveSearchField($attribute)
{
$tableSchema = $this->getTableSchema();
$column = $tableSchema->columns[$attribute];
if ($column->phpType === 'boolean') {
return "\$form->field(\$model, '$attribute')->checkbox();";
} else {
return "\$form->field(\$model, '$attribute');";
}
}
/**
* @param \yii\db\ColumnSchema $column * @param \yii\db\ColumnSchema $column
* @return string * @return string
*/ */
...@@ -256,4 +244,146 @@ class Generator extends \yii\gii\Generator ...@@ -256,4 +244,146 @@ class Generator extends \yii\gii\Generator
return 'text'; return 'text';
} }
} }
/**
* Generates validation rules for the search model.
* @return array the generated validation rules
*/
public function generateSearchRules()
{
$table = $this->getTableSchema();
$types = array();
foreach ($table->columns as $column) {
switch ($column->type) {
case Schema::TYPE_SMALLINT:
case Schema::TYPE_INTEGER:
case Schema::TYPE_BIGINT:
$types['integer'][] = $column->name;
break;
case Schema::TYPE_BOOLEAN:
$types['boolean'][] = $column->name;
break;
case Schema::TYPE_FLOAT:
case Schema::TYPE_DECIMAL:
case Schema::TYPE_MONEY:
$types['number'][] = $column->name;
break;
case Schema::TYPE_DATE:
case Schema::TYPE_TIME:
case Schema::TYPE_DATETIME:
case Schema::TYPE_TIMESTAMP:
default:
$types['safe'][] = $column->name;
break;
}
}
$rules = array();
foreach ($types as $type => $columns) {
$rules[] = "array('" . implode(', ', $columns) . "', '$type')";
}
return $rules;
}
public function getSearchAttributes()
{
return $this->getTableSchema()->getColumnNames();
}
/**
* Generates the attribute labels for the search model.
* @return array the generated attribute labels (name => label)
*/
public function generateSearchLabels()
{
$table = $this->getTableSchema();
$labels = array();
foreach ($table->columns as $column) {
if (!strcasecmp($column->name, 'id')) {
$labels[$column->name] = 'ID';
} else {
$label = Inflector::camel2words($column->name);
if (strcasecmp(substr($label, -3), ' id') === 0) {
$label = substr($label, 0, -3) . ' ID';
}
$labels[$column->name] = $label;
}
}
return $labels;
}
public function generateSearchConditions()
{
$table = $this->getTableSchema();
$conditions = array();
foreach ($table->columns as $column) {
switch ($column->type) {
case Schema::TYPE_SMALLINT:
case Schema::TYPE_INTEGER:
case Schema::TYPE_BIGINT:
case Schema::TYPE_BOOLEAN:
case Schema::TYPE_FLOAT:
case Schema::TYPE_DECIMAL:
case Schema::TYPE_MONEY:
case Schema::TYPE_DATE:
case Schema::TYPE_TIME:
case Schema::TYPE_DATETIME:
case Schema::TYPE_TIMESTAMP:
$conditions[] = "\$this->addCondition(\$query, '{$column->name}');";
break;
default:
$conditions[] = "\$this->addCondition(\$query, '{$column->name}', true);";
break;
}
}
return $conditions;
}
public function generateUrlParams()
{
$pks = $this->getTableSchema()->primaryKey;
if (count($pks) === 1) {
return "'id' => \$model->{$pks[0]}";
} else {
$params = array();
foreach ($pks as $pk) {
$params[] = "'$pk' => \$model->$pk";
}
return implode(', ', $params);
}
}
public function generateActionParams()
{
$pks = $this->getTableSchema()->primaryKey;
if (count($pks) === 1) {
return '$id';
} else {
return '$' . implode(', $', $pks);
}
}
public function generateActionParamComments()
{
$table = $this->getTableSchema();
$pks = $table->primaryKey;
if (count($pks) === 1) {
return array('@param ' . $table->columns[$pks[0]]->phpType . ' $id');
} else {
$params = array();
foreach ($pks as $pk) {
$params[] = '@param ' . $table->columns[$pk]->phpType . ' $' . $pk;
}
return $params;
}
}
public function getTableSchema()
{
/** @var ActiveRecord $class */
$class = $this->modelClass;
return $class::getTableSchema();
}
} }
...@@ -6,11 +6,10 @@ ...@@ -6,11 +6,10 @@
*/ */
echo $form->field($generator, 'modelClass'); echo $form->field($generator, 'modelClass');
echo $form->field($generator, 'searchModelClass');
echo $form->field($generator, 'controllerClass'); echo $form->field($generator, 'controllerClass');
echo $form->field($generator, 'baseControllerClass'); echo $form->field($generator, 'baseControllerClass');
echo $form->field($generator, 'moduleID'); echo $form->field($generator, 'moduleID');
echo $form->field($generator, 'enableSearch')->checkbox();
echo $form->field($generator, 'searchModelClass');
echo $form->field($generator, 'indexWidgetType')->dropDownList(array( echo $form->field($generator, 'indexWidgetType')->dropDownList(array(
'grid' => 'GridView', 'grid' => 'GridView',
'list' => 'ListView', 'list' => 'ListView',
......
...@@ -9,38 +9,22 @@ use yii\helpers\StringHelper; ...@@ -9,38 +9,22 @@ use yii\helpers\StringHelper;
* @var yii\gii\generators\crud\Generator $generator * @var yii\gii\generators\crud\Generator $generator
*/ */
$pos = strrpos($generator->controllerClass, '\\'); $controllerClass = StringHelper::basename($generator->controllerClass);
$ns = ltrim(substr($generator->controllerClass, 0, $pos), '\\'); $modelClass = StringHelper::basename($generator->modelClass);
$controllerClass = substr($generator->controllerClass, $pos + 1); $searchModelClass = StringHelper::basename($generator->searchModelClass);
$pos = strrpos($generator->modelClass, '\\');
$modelClass = $pos === false ? $generator->modelClass : substr($generator->modelClass, $pos + 1); $pks = $generator->getTableSchema()->primaryKey;
$urlParams = $generator->generateUrlParams();
/** @var \yii\db\ActiveRecord $class */ $actionParams = $generator->generateActionParams();
$class = $generator->modelClass; $actionParamComments = $generator->generateActionParamComments();
$pks = $class::primaryKey();
$schema = $class::getTableSchema();
if (count($pks) === 1) {
$ids = '$id';
$params = "array('id' => \$model->{$pks[0]})";
$paramComments = '@param ' . $schema->columns[$pks[0]]->phpType . ' $id';
} else {
$ids = '$' . implode(', $', $pks);
$params = array();
$paramComments = array();
foreach ($pks as $pk) {
$paramComments[] = '@param ' . $schema->columns[$pk]->phpType . ' $' . $pk;
$params[] = "'$pk' => \$model->$pk";
}
$params = implode(', ', $params);
$paramComments = implode("\n\t * ", $paramComments);
}
echo "<?php\n"; echo "<?php\n";
?> ?>
namespace <?php echo $ns; ?>; namespace <?php echo StringHelper::dirname(ltrim($generator->controllerClass, '\\')); ?>;
use <?php echo ltrim($generator->modelClass, '\\'); ?>; use <?php echo ltrim($generator->modelClass, '\\'); ?>;
use <?php echo ltrim($generator->searchModelClass, '\\'); ?>;
use yii\data\ActiveDataProvider; use yii\data\ActiveDataProvider;
use <?php echo ltrim($generator->baseControllerClass, '\\'); ?>; use <?php echo ltrim($generator->baseControllerClass, '\\'); ?>;
use yii\web\HttpException; use yii\web\HttpException;
...@@ -56,23 +40,24 @@ class <?php echo $controllerClass; ?> extends <?php echo StringHelper::basename( ...@@ -56,23 +40,24 @@ class <?php echo $controllerClass; ?> extends <?php echo StringHelper::basename(
*/ */
public function actionIndex() public function actionIndex()
{ {
$dataProvider = new ActiveDataProvider(array( $searchModel = new <?php echo $searchModelClass; ?>;
'query' => <?php echo $modelClass; ?>::find(), $dataProvider = $searchModel->search($_GET);
));
return $this->render('index', array( return $this->render('index', array(
'dataProvider' => $dataProvider, 'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
)); ));
} }
/** /**
* Displays a single <?php echo $modelClass; ?> model. * Displays a single <?php echo $modelClass; ?> model.
* <?php echo $paramComments . "\n"; ?> * <?php echo implode("\n\t * ", $actionParamComments) . "\n"; ?>
* @return mixed * @return mixed
*/ */
public function actionView(<?php echo $ids; ?>) public function actionView(<?php echo $actionParams; ?>)
{ {
return $this->render('view', array( return $this->render('view', array(
'model' => $this->findModel(<?php echo $ids; ?>), 'model' => $this->findModel(<?php echo $actionParams; ?>),
)); ));
} }
...@@ -86,7 +71,7 @@ class <?php echo $controllerClass; ?> extends <?php echo StringHelper::basename( ...@@ -86,7 +71,7 @@ class <?php echo $controllerClass; ?> extends <?php echo StringHelper::basename(
$model = new <?php echo $modelClass; ?>; $model = new <?php echo $modelClass; ?>;
if ($model->load($_POST) && $model->save()) { if ($model->load($_POST) && $model->save()) {
return $this->redirect(array('view', <?php echo $params; ?>)); return $this->redirect(array('view', <?php echo $urlParams; ?>));
} else { } else {
return $this->render('create', array( return $this->render('create', array(
'model' => $model, 'model' => $model,
...@@ -97,15 +82,15 @@ class <?php echo $controllerClass; ?> extends <?php echo StringHelper::basename( ...@@ -97,15 +82,15 @@ class <?php echo $controllerClass; ?> extends <?php echo StringHelper::basename(
/** /**
* Updates an existing <?php echo $modelClass; ?> model. * Updates an existing <?php echo $modelClass; ?> model.
* If update is successful, the browser will be redirected to the 'view' page. * If update is successful, the browser will be redirected to the 'view' page.
* <?php echo $paramComments . "\n"; ?> * <?php echo implode("\n\t * ", $actionParamComments) . "\n"; ?>
* @return mixed * @return mixed
*/ */
public function actionUpdate(<?php echo $ids; ?>) public function actionUpdate(<?php echo $actionParams; ?>)
{ {
$model = $this->findModel(<?php echo $ids; ?>); $model = $this->findModel(<?php echo $actionParams; ?>);
if ($model->load($_POST) && $model->save()) { if ($model->load($_POST) && $model->save()) {
return $this->redirect(array('view', <?php echo $params; ?>)); return $this->redirect(array('view', <?php echo $urlParams; ?>));
} else { } else {
return $this->render('update', array( return $this->render('update', array(
'model' => $model, 'model' => $model,
...@@ -116,23 +101,23 @@ class <?php echo $controllerClass; ?> extends <?php echo StringHelper::basename( ...@@ -116,23 +101,23 @@ class <?php echo $controllerClass; ?> extends <?php echo StringHelper::basename(
/** /**
* Deletes an existing <?php echo $modelClass; ?> model. * Deletes an existing <?php echo $modelClass; ?> model.
* If deletion is successful, the browser will be redirected to the 'index' page. * If deletion is successful, the browser will be redirected to the 'index' page.
* <?php echo $paramComments . "\n"; ?> * <?php echo implode("\n\t * ", $actionParamComments) . "\n"; ?>
* @return mixed * @return mixed
*/ */
public function actionDelete(<?php echo $ids; ?>) public function actionDelete(<?php echo $actionParams; ?>)
{ {
$this->findModel(<?php echo $ids; ?>)->delete(); $this->findModel(<?php echo $actionParams; ?>)->delete();
return $this->redirect(array('index')); return $this->redirect(array('index'));
} }
/** /**
* Finds the <?php echo $modelClass; ?> model based on its primary key value. * Finds the <?php echo $modelClass; ?> model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown. * If the model is not found, a 404 HTTP exception will be thrown.
* <?php echo $paramComments . "\n"; ?> * <?php echo implode("\n\t * ", $actionParamComments) . "\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
*/ */
protected function findModel(<?php echo $ids; ?>) protected function findModel(<?php echo $actionParams; ?>)
{ {
<?php <?php
if (count($pks) === 1) { if (count($pks) === 1) {
......
<?php <?php
use yii\helpers\StringHelper;
/** /**
* Created by JetBrains PhpStorm. * This is the template for generating a CRUD controller class file.
* User: qiang *
* Date: 8/26/13 * @var yii\base\View $this
* Time: 5:22 PM * @var yii\gii\generators\crud\Generator $generator
* To change this template use File | Settings | File Templates.
*/ */
$modelClass = StringHelper::basename($generator->modelClass);
$searchModelClass = StringHelper::basename($generator->searchModelClass);
$rules = $generator->generateSearchRules();
$labels = $generator->generateSearchLabels();
$searchAttributes = $generator->getSearchAttributes();
$searchConditions = $generator->generateSearchConditions();
echo "<?php\n";
?>
namespace <?php echo StringHelper::dirname(ltrim($generator->searchModelClass, '\\')); ?>;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use <?php echo ltrim($generator->modelClass, '\\'); ?>;
/**
* <?php echo $searchModelClass; ?> represents the model behind the search form about <?php echo $modelClass; ?>.
*/
class <?php echo $searchModelClass; ?> extends Model
{
public $<?php echo implode(";\n\tpublic $", $searchAttributes); ?>;
public function rules()
{
return array(
<?php echo implode(",\n\t\t\t", $rules); ?>,
);
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return array(
<?php foreach ($labels as $name => $label): ?>
<?php echo "'$name' => '" . addslashes($label) . "',\n"; ?>
<?php endforeach; ?>
);
}
public function search($params)
{
$query = <?php echo $modelClass; ?>::find();
$dataProvider = new ActiveDataProvider(array(
'query' => $query,
));
if (!($this->load($params) && $this->validate())) {
return $dataProvider;
}
<?php echo implode("\n\t\t", $searchConditions); ?>
return $dataProvider;
}
protected function addCondition($query, $attribute, $partialMatch = false)
{
$value = $this->$attribute;
if (trim($value) === '') {
return;
}
if ($partialMatch) {
$value = '%' . strtr($value, array('%'=>'\%', '_'=>'\_', '\\'=>'\\\\')) . '%';
$query->andWhere(array('like', $attribute, $value));
} else {
$query->andWhere(array($attribute => $value));
}
}
}
...@@ -9,8 +9,7 @@ use yii\helpers\StringHelper; ...@@ -9,8 +9,7 @@ use yii\helpers\StringHelper;
*/ */
/** @var \yii\db\ActiveRecord $model */ /** @var \yii\db\ActiveRecord $model */
$class = $generator->modelClass; $model = new $generator->modelClass;
$model = new $class;
$safeAttributes = $model->safeAttributes(); $safeAttributes = $model->safeAttributes();
if (empty($safeAttributes)) { if (empty($safeAttributes)) {
$safeAttributes = $model->getTableSchema()->columnNames; $safeAttributes = $model->getTableSchema()->columnNames;
...@@ -34,7 +33,7 @@ use yii\widgets\ActiveForm; ...@@ -34,7 +33,7 @@ use yii\widgets\ActiveForm;
<?php echo '<?php'; ?> $form = ActiveForm::begin(); ?> <?php echo '<?php'; ?> $form = ActiveForm::begin(); ?>
<?php foreach ($safeAttributes as $attribute) { <?php foreach ($safeAttributes as $attribute) {
echo "\t\t<?php echo " . $generator->generateActiveField($model, $attribute) . " ?>\n\n"; echo "\t\t<?php echo " . $generator->generateActiveField($attribute) . " ?>\n\n";
} ?> } ?>
<div class="form-group"> <div class="form-group">
<?php echo '<?php'; ?> echo Html::submitButton($model->isNewRecord ? 'Create' : 'Update', array('class' => 'btn btn-primary')); ?> <?php echo '<?php'; ?> echo Html::submitButton($model->isNewRecord ? 'Create' : 'Update', array('class' => 'btn btn-primary')); ?>
......
<?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
*/ */
echo "<?php\n";
?> ?>
<?php echo "<?php\n"; ?>
/* @var $this <?php echo $this->getControllerClass(); ?> */ use yii\helpers\Html;
/* @var $model <?php echo $this->getModelClass(); ?> */ use yii\widgets\ActiveForm;
/* @var $form CActiveForm */
/**
* @var yii\base\View $this
* @var <?php echo ltrim($generator->searchModelClass, '\\'); ?> $model
* @var yii\widgets\ActiveForm $form
*/
?> ?>
<div class="wide form"> <div class="<?php echo Inflector::camel2id(StringHelper::basename($generator->modelClass)); ?>-search">
<?php echo "<?php \$form=\$this->beginWidget('CActiveForm', array( <?php echo '<?php'; ?> $form = ActiveForm::begin(array('method' => 'get')); ?>
'action'=>Yii::app()->createUrl(\$this->route),
'method'=>'get',
)); ?>\n"; ?>
<?php foreach($this->tableSchema->columns as $column): ?>
<?php <?php
$field=$this->generateInputField($this->modelClass,$column); $count = 0;
if(strpos($field,'password')!==false) foreach ($generator->getTableSchema()->getColumnNames() as $attribute) {
continue; if (++$count < 6) {
echo "\t\t<?php echo " . $generator->generateActiveSearchField($attribute) . " ?>\n";
} else {
echo "\t\t<?php // echo " . $generator->generateActiveSearchField($attribute) . " ?>\n";
}
}
?> ?>
<div class="row"> <div class="form-group">
<?php echo "<?php echo \$form->label(\$model,'{$column->name}'); ?>\n"; ?> <?php echo '<?php'; ?> echo Html::submitButton('Search', array('class' => 'btn btn-primary')); ?>
<?php echo "<?php echo ".$this->generateActiveField($this->modelClass,$column)."; ?>\n"; ?> <?php echo '<?php'; ?> echo Html::resetButton('Reset', array('class' => 'btn btn-default')); ?>
</div>
<?php endforeach; ?>
<div class="row buttons">
<?php echo "<?php echo CHtml::submitButton('Search'); ?>\n"; ?>
</div> </div>
<?php echo "<?php \$this->endWidget(); ?>\n"; ?> <?php echo '<?php'; ?> ActiveForm::end(); ?>
</div><!-- search-form --> </div>
\ No newline at end of file
...@@ -19,6 +19,8 @@ use yii\helpers\Html; ...@@ -19,6 +19,8 @@ use yii\helpers\Html;
*/ */
$this->title = 'Create <?php echo Inflector::camel2words(StringHelper::basename($generator->modelClass)); ?>'; $this->title = 'Create <?php echo Inflector::camel2words(StringHelper::basename($generator->modelClass)); ?>';
$this->params['breadcrumbs'][] = array('label' => '<?php echo Inflector::pluralize(Inflector::camel2words(StringHelper::basename($generator->modelClass))); ?>', 'url' => array('index'));
$this->params['breadcrumbs'][] = $this->title;
?> ?>
<div class="<?php echo Inflector::camel2id(StringHelper::basename($generator->modelClass)); ?>-create"> <div class="<?php echo Inflector::camel2id(StringHelper::basename($generator->modelClass)); ?>-create">
......
...@@ -8,19 +8,7 @@ use yii\helpers\StringHelper; ...@@ -8,19 +8,7 @@ use yii\helpers\StringHelper;
* @var yii\gii\generators\crud\Generator $generator * @var yii\gii\generators\crud\Generator $generator
*/ */
/** @var \yii\db\ActiveRecord $model */ $urlParams = $generator->generateUrlParams();
$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(); $nameAttribute = $generator->getNameAttribute();
echo "<?php\n"; echo "<?php\n";
...@@ -32,21 +20,50 @@ use <?php echo $generator->indexWidgetType === 'grid' ? 'yii\grid\GridView' : 'y ...@@ -32,21 +20,50 @@ use <?php echo $generator->indexWidgetType === 'grid' ? 'yii\grid\GridView' : 'y
/** /**
* @var yii\base\View $this * @var yii\base\View $this
* @var yii\data\ActiveDataProvider $dataProvider * @var yii\data\ActiveDataProvider $dataProvider
* @var <?php echo ltrim($generator->searchModelClass, '\\'); ?> $searchModel
*/ */
$this->title = '<?php echo Inflector::pluralize(Inflector::camel2words(StringHelper::basename($generator->modelClass))); ?>'; $this->title = '<?php echo Inflector::pluralize(Inflector::camel2words(StringHelper::basename($generator->modelClass))); ?>';
$this->params['breadcrumbs'][] = $this->title;
?> ?>
<div class="<?php echo Inflector::camel2id(StringHelper::basename($generator->modelClass)); ?>-index"> <div class="<?php echo Inflector::camel2id(StringHelper::basename($generator->modelClass)); ?>-index">
<h1><?php echo "<?php"; ?> echo Html::encode($this->title); ?></h1> <h1><?php echo "<?php"; ?> echo Html::encode($this->title); ?></h1>
<?php if ($generator->indexWidgetType === 'grid'): ?> <?php echo '<?php'; ?> echo $this->render('_search', array('model' => $searchModel)); ?>
<hr>
<div>
<?php echo '<?php'; ?> echo Html::a('Create <?php echo StringHelper::basename($generator->modelClass); ?>', array('create'), array('class' => 'btn btn-danger')); ?>
</div>
<?php if ($generator->indexWidgetType === 'grid'): ?>
<?php echo "<?php"; ?> echo GridView::widget(array(
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => array(
<?php
$count = 0;
foreach ($generator->getTableSchema()->columns as $column) {
$format = $generator->generateColumnFormat($column);
if (++$count < 6) {
echo "\t\t\t'" . $column->name . ($format === 'text' ? '' : ':' . $format) . "',\n";
} else {
echo "\t\t\t// '" . $column->name . ($format === 'text' ? '' : ':' . $format) . "',\n";
}
}
?>
),
)); ?>
<?php else: ?> <?php else: ?>
<?php echo "\t<?php"; ?> echo ListView::widget(array( <?php echo "<?php"; ?> echo ListView::widget(array(
'dataProvider' => $dataProvider, 'dataProvider' => $dataProvider,
'itemOptions' => array(
'class' => 'item',
),
'itemView' => function ($model, $key, $index, $widget) { 'itemView' => function ($model, $key, $index, $widget) {
return Html::a(Html::encode($model-><?php echo $nameAttribute; ?>), <?php echo $viewUrl; ?>); return Html::a(Html::encode($model-><?php echo $nameAttribute; ?>), array('view', <?php echo $urlParams; ?>);
}, },
)); ?> )); ?>
<?php endif; ?> <?php endif; ?>
......
...@@ -8,6 +8,8 @@ use yii\helpers\StringHelper; ...@@ -8,6 +8,8 @@ use yii\helpers\StringHelper;
* @var yii\gii\generators\crud\Generator $generator * @var yii\gii\generators\crud\Generator $generator
*/ */
$urlParams = $generator->generateUrlParams();
echo "<?php\n"; echo "<?php\n";
?> ?>
...@@ -19,6 +21,9 @@ use yii\helpers\Html; ...@@ -19,6 +21,9 @@ use yii\helpers\Html;
*/ */
$this->title = 'Update <?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(); ?>;
$this->params['breadcrumbs'][] = array('label' => '<?php echo Inflector::pluralize(Inflector::camel2words(StringHelper::basename($generator->modelClass))); ?>', 'url' => array('index'));
$this->params['breadcrumbs'][] = array('label' => $model-><?php echo $generator->getNameAttribute(); ?>, 'url' => array('view', <?php echo $urlParams; ?>));
$this->params['breadcrumbs'][] = 'Update';
?> ?>
<div class="<?php echo Inflector::camel2id(StringHelper::basename($generator->modelClass)); ?>-update"> <div class="<?php echo Inflector::camel2id(StringHelper::basename($generator->modelClass)); ?>-update">
......
...@@ -8,9 +8,7 @@ use yii\helpers\StringHelper; ...@@ -8,9 +8,7 @@ use yii\helpers\StringHelper;
* @var yii\gii\generators\crud\Generator $generator * @var yii\gii\generators\crud\Generator $generator
*/ */
/** @var \yii\db\ActiveRecord $model */ $urlParams = $generator->generateUrlParams();
$class = $generator->modelClass;
$model = new $class;
echo "<?php\n"; echo "<?php\n";
?> ?>
...@@ -24,16 +22,23 @@ use yii\widgets\DetailView; ...@@ -24,16 +22,23 @@ use yii\widgets\DetailView;
*/ */
$this->title = $model-><?php echo $generator->getNameAttribute(); ?>; $this->title = $model-><?php echo $generator->getNameAttribute(); ?>;
$this->params['breadcrumbs'][] = array('label' => '<?php echo Inflector::pluralize(Inflector::camel2words(StringHelper::basename($generator->modelClass))); ?>', 'url' => array('index'));
$this->params['breadcrumbs'][] = $this->title;
?> ?>
<div class="<?php echo Inflector::camel2id(StringHelper::basename($generator->modelClass)); ?>-view"> <div class="<?php echo Inflector::camel2id(StringHelper::basename($generator->modelClass)); ?>-view">
<h1><?php echo "<?php"; ?> echo Html::encode($this->title); ?></h1> <h1><?php echo "<?php"; ?> echo Html::encode($this->title); ?></h1>
<div>
<?php echo '<?php'; ?> echo Html::a('Update', array('update', <?php echo $urlParams; ?>), array('class' => 'btn btn-danger')); ?>
<?php echo '<?php'; ?> echo Html::a('Delete', array('delete', <?php echo $urlParams; ?>), array('class' => 'btn btn-danger')); ?>
</div>
<?php echo '<?php'; ?> echo DetailView::widget(array( <?php echo '<?php'; ?> echo DetailView::widget(array(
'model' => $model, 'model' => $model,
'attributes' => array( 'attributes' => array(
<?php <?php
foreach ($model->getTableSchema()->columns as $column) { foreach ($generator->getTableSchema()->columns as $column) {
$format = $generator->generateColumnFormat($column); $format = $generator->generateColumnFormat($column);
echo "\t\t\t'" . $column->name . ($format === 'text' ? '' : ':' . $format) . "',\n"; echo "\t\t\t'" . $column->name . ($format === 'text' ? '' : ':' . $format) . "',\n";
} }
......
...@@ -40,7 +40,7 @@ foreach ($generator->templates as $name => $path) { ...@@ -40,7 +40,7 @@ foreach ($generator->templates as $name => $path) {
'form' => $form, 'form' => $form,
)); ?> )); ?>
<?php echo $form->field($generator, 'template')->sticky() <?php echo $form->field($generator, 'template')->sticky()
->label(array('label' => 'Code Template')) ->label('Code Template')
->dropDownList($templates)->hint(' ->dropDownList($templates)->hint('
Please select which set of the templates should be used to generated the code. Please select which set of the templates should be used to generated the code.
'); ?> '); ?>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment