Commit 6f9aee90 by johonunu

Search model not required anymore in crud generator

parent 2e892c42
...@@ -35,7 +35,7 @@ class Generator extends \yii\gii\Generator ...@@ -35,7 +35,7 @@ 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 $searchModelClass; public $searchModelClass = '';
/** /**
* @inheritdoc * @inheritdoc
...@@ -61,7 +61,7 @@ class Generator extends \yii\gii\Generator ...@@ -61,7 +61,7 @@ class Generator extends \yii\gii\Generator
{ {
return array_merge(parent::rules(), [ return array_merge(parent::rules(), [
[['moduleID', 'controllerClass', 'modelClass', 'searchModelClass', 'baseControllerClass'], 'filter', 'filter' => 'trim'], [['moduleID', 'controllerClass', 'modelClass', 'searchModelClass', 'baseControllerClass'], 'filter', 'filter' => 'trim'],
[['modelClass', 'searchModelClass', 'controllerClass', 'baseControllerClass', 'indexWidgetType'], 'required'], [['modelClass', 'controllerClass', 'baseControllerClass', 'indexWidgetType'], 'required'],
[['searchModelClass'], 'compare', 'compareAttribute' => 'modelClass', 'operator' => '!==', 'message' => 'Search Model Class must not be equal to Model Class.'], [['searchModelClass'], 'compare', 'compareAttribute' => 'modelClass', 'operator' => '!==', 'message' => 'Search Model Class must not be equal to Model Class.'],
[['modelClass', 'controllerClass', 'baseControllerClass', 'searchModelClass'], 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'], [['modelClass', 'controllerClass', 'baseControllerClass', 'searchModelClass'], 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'],
[['modelClass'], 'validateClass', 'params' => ['extends' => BaseActiveRecord::className()]], [['modelClass'], 'validateClass', 'params' => ['extends' => BaseActiveRecord::className()]],
...@@ -162,15 +162,20 @@ class Generator extends \yii\gii\Generator ...@@ -162,15 +162,20 @@ class Generator extends \yii\gii\Generator
public function generate() public function generate()
{ {
$controllerFile = Yii::getAlias('@' . str_replace('\\', '/', ltrim($this->controllerClass, '\\')) . '.php'); $controllerFile = Yii::getAlias('@' . str_replace('\\', '/', ltrim($this->controllerClass, '\\')) . '.php');
$searchModel = Yii::getAlias('@' . str_replace('\\', '/', ltrim($this->searchModelClass, '\\') . '.php'));
$files = [ $files = [
new CodeFile($controllerFile, $this->render('controller.php')), new CodeFile($controllerFile, $this->render('controller.php')),
new CodeFile($searchModel, $this->render('search.php')),
]; ];
if(!empty($this->searchModelClass)){
$searchModel = Yii::getAlias('@' . str_replace('\\', '/', ltrim($this->searchModelClass, '\\') . '.php'));
$files[] = 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(empty($this->searchModelClass) && $file==='_search.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"));
} }
......
...@@ -31,7 +31,11 @@ namespace <?= StringHelper::dirname(ltrim($generator->controllerClass, '\\')) ?> ...@@ -31,7 +31,11 @@ namespace <?= StringHelper::dirname(ltrim($generator->controllerClass, '\\')) ?>
use Yii; use Yii;
use <?= ltrim($generator->modelClass, '\\') ?>; use <?= ltrim($generator->modelClass, '\\') ?>;
<?php if(!empty($generator->searchModelClass)){ ?>
use <?= ltrim($generator->searchModelClass, '\\') . (isset($searchModelAlias) ? " as $searchModelAlias" : "") ?>; use <?= ltrim($generator->searchModelClass, '\\') . (isset($searchModelAlias) ? " as $searchModelAlias" : "") ?>;
<?php }else{ ?>
use yii\data\ActiveDataProvider;
<?php } ?>
use <?= ltrim($generator->baseControllerClass, '\\') ?>; use <?= ltrim($generator->baseControllerClass, '\\') ?>;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter; use yii\filters\VerbFilter;
...@@ -59,6 +63,7 @@ class <?= $controllerClass ?> extends <?= StringHelper::basename($generator->bas ...@@ -59,6 +63,7 @@ class <?= $controllerClass ?> extends <?= StringHelper::basename($generator->bas
*/ */
public function actionIndex() public function actionIndex()
{ {
<?php if(!empty($generator->searchModelClass)){ ?>
$searchModel = new <?= isset($searchModelAlias) ? $searchModelAlias : $searchModelClass ?>; $searchModel = new <?= isset($searchModelAlias) ? $searchModelAlias : $searchModelClass ?>;
$dataProvider = $searchModel->search(Yii::$app->request->getQueryParams()); $dataProvider = $searchModel->search(Yii::$app->request->getQueryParams());
...@@ -66,6 +71,15 @@ class <?= $controllerClass ?> extends <?= StringHelper::basename($generator->bas ...@@ -66,6 +71,15 @@ class <?= $controllerClass ?> extends <?= StringHelper::basename($generator->bas
'dataProvider' => $dataProvider, 'dataProvider' => $dataProvider,
'searchModel' => $searchModel, 'searchModel' => $searchModel,
]); ]);
<?php }else{ ?>
$dataProvider = new ActiveDataProvider([
'query' => <?= $modelClass ?>::find(),
]);
return $this->render('index', [
'dataProvider' => $dataProvider,
]);
<?php } ?>
} }
/** /**
......
...@@ -20,7 +20,7 @@ use <?= $generator->indexWidgetType === 'grid' ? "yii\\grid\\GridView" : "yii\\w ...@@ -20,7 +20,7 @@ use <?= $generator->indexWidgetType === 'grid' ? "yii\\grid\\GridView" : "yii\\w
/** /**
* @var yii\web\View $this * @var yii\web\View $this
* @var yii\data\ActiveDataProvider $dataProvider * @var yii\data\ActiveDataProvider $dataProvider
* @var <?= ltrim($generator->searchModelClass, '\\') ?> $searchModel <?= !empty($generator->searchModelClass)?" * @var ".ltrim($generator->searchModelClass, '\\')." \$searchModel\n":'' ?>
*/ */
$this->title = <?= $generator->generateString(Inflector::pluralize(Inflector::camel2words(StringHelper::basename($generator->modelClass)))) ?>; $this->title = <?= $generator->generateString(Inflector::pluralize(Inflector::camel2words(StringHelper::basename($generator->modelClass)))) ?>;
...@@ -29,8 +29,7 @@ $this->params['breadcrumbs'][] = $this->title; ...@@ -29,8 +29,7 @@ $this->params['breadcrumbs'][] = $this->title;
<div class="<?= Inflector::camel2id(StringHelper::basename($generator->modelClass)) ?>-index"> <div class="<?= Inflector::camel2id(StringHelper::basename($generator->modelClass)) ?>-index">
<h1><?= "<?= " ?>Html::encode($this->title) ?></h1> <h1><?= "<?= " ?>Html::encode($this->title) ?></h1>
<?php if(!empty($generator->searchModelClass)){ ?><?= "<?php " . ($generator->indexWidgetType === 'grid' ? "// " : "") ?>echo $this->render('_search', ['model' => $searchModel]); ?><?php } ?>
<?= "<?php " . ($generator->indexWidgetType === 'grid' ? "// " : "") ?>echo $this->render('_search', ['model' => $searchModel]); ?>
<p> <p>
<?= "<?= " ?>Html::a(<?= $generator->generateString('Create {modelClass}', ['modelClass' => Inflector::camel2words(StringHelper::basename($generator->modelClass))]) ?>, ['create'], ['class' => 'btn btn-success']) ?> <?= "<?= " ?>Html::a(<?= $generator->generateString('Create {modelClass}', ['modelClass' => Inflector::camel2words(StringHelper::basename($generator->modelClass))]) ?>, ['create'], ['class' => 'btn btn-success']) ?>
...@@ -39,8 +38,7 @@ $this->params['breadcrumbs'][] = $this->title; ...@@ -39,8 +38,7 @@ $this->params['breadcrumbs'][] = $this->title;
<?php if ($generator->indexWidgetType === 'grid'): ?> <?php if ($generator->indexWidgetType === 'grid'): ?>
<?= "<?= " ?>GridView::widget([ <?= "<?= " ?>GridView::widget([
'dataProvider' => $dataProvider, 'dataProvider' => $dataProvider,
'filterModel' => $searchModel, <?= !empty($generator->searchModelClass)?"'filterModel' => \$searchModel,\n\t\t'columns' => [\n":"'columns' => [\n"; ?>
'columns' => [
['class' => 'yii\grid\SerialColumn'], ['class' => 'yii\grid\SerialColumn'],
<?php <?php
......
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