diff --git a/extensions/gii/generators/crud/Generator.php b/extensions/gii/generators/crud/Generator.php index 1925de2..4b31d70 100644 --- a/extensions/gii/generators/crud/Generator.php +++ b/extensions/gii/generators/crud/Generator.php @@ -9,6 +9,7 @@ namespace yii\gii\generators\crud; use Yii; use yii\db\ActiveRecord; +use yii\db\BaseActiveRecord; use yii\db\Schema; use yii\gii\CodeFile; use yii\helpers\Inflector; @@ -46,7 +47,7 @@ class Generator extends \yii\gii\Generator [['modelClass', 'searchModelClass', 'controllerClass', 'baseControllerClass', 'indexWidgetType'], 'required'], [['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'], 'validateClass', 'params' => ['extends' => ActiveRecord::className()]], + [['modelClass'], 'validateClass', 'params' => ['extends' => BaseActiveRecord::className()]], [['baseControllerClass'], 'validateClass', 'params' => ['extends' => Controller::className()]], [['controllerClass'], 'match', 'pattern' => '/Controller$/', 'message' => 'Controller class name must be suffixed with "Controller".'], [['controllerClass', 'searchModelClass'], 'validateNewClass'], @@ -167,13 +168,13 @@ class Generator extends \yii\gii\Generator public function getNameAttribute() { - /** @var \yii\db\ActiveRecord $class */ - $class = $this->modelClass; - foreach ($class::getTableSchema()->columnNames as $name) { + foreach ($this->getColumnNames() as $name) { if (!strcasecmp($name, 'name') || !strcasecmp($name, 'title')) { return $name; } } + /** @var \yii\db\ActiveRecord $class */ + $class = $this->modelClass; $pk = $class::primaryKey(); return $pk[0]; } @@ -185,8 +186,12 @@ class Generator extends \yii\gii\Generator public function generateActiveField($attribute) { $tableSchema = $this->getTableSchema(); - if (!isset($tableSchema->columns[$attribute])) { - return "\$form->field(\$model, '$attribute');"; + if ($tableSchema === false || !isset($tableSchema->columns[$attribute])) { + if (preg_match('/^(password|pass|passwd|passcode)$/i', $attribute)) { + return "\$form->field(\$model, '$attribute')->passwordInput();"; + } else { + return "\$form->field(\$model, '$attribute');"; + } } $column = $tableSchema->columns[$attribute]; if ($column->phpType === 'boolean') { @@ -214,6 +219,9 @@ class Generator extends \yii\gii\Generator public function generateActiveSearchField($attribute) { $tableSchema = $this->getTableSchema(); + if ($tableSchema === false) { + return "\$form->field(\$model, '$attribute')"; + } $column = $tableSchema->columns[$attribute]; if ($column->phpType === 'boolean') { return "\$form->field(\$model, '$attribute')->checkbox()"; @@ -249,7 +257,9 @@ class Generator extends \yii\gii\Generator */ public function generateSearchRules() { - $table = $this->getTableSchema(); + if (($table = $this->getTableSchema()) === false) { + return ["[['" . implode("', '", $this->getColumnNames()) . "'], 'safe']"]; + } $types = []; foreach ($table->columns as $column) { switch ($column->type) { @@ -286,7 +296,7 @@ class Generator extends \yii\gii\Generator public function getSearchAttributes() { - return $this->getTableSchema()->getColumnNames(); + return $this->getColumnNames(); } /** @@ -295,17 +305,16 @@ class Generator extends \yii\gii\Generator */ public function generateSearchLabels() { - $table = $this->getTableSchema(); $labels = []; - foreach ($table->columns as $column) { - if (!strcasecmp($column->name, 'id')) { - $labels[$column->name] = 'ID'; + foreach ($this->getColumnNames() as $name) { + if (!strcasecmp($name, 'id')) { + $labels[$name] = 'ID'; } else { - $label = Inflector::camel2words($column->name); + $label = Inflector::camel2words($name); if (strcasecmp(substr($label, -3), ' id') === 0) { $label = substr($label, 0, -3) . ' ID'; } - $labels[$column->name] = $label; + $labels[$name] = $label; } } return $labels; @@ -313,10 +322,21 @@ class Generator extends \yii\gii\Generator public function generateSearchConditions() { - $table = $this->getTableSchema(); + $columns = []; + if (($table = $this->getTableSchema()) === false) { + $class = $this->modelClass; + $model = new $class(); + foreach ($model->attributes() as $attribute) { + $columns[$attribute] = 'unknown'; + } + } else { + foreach ($table->columns as $column) { + $columns[$column->name] = $column->type; + } + } $conditions = []; - foreach ($table->columns as $column) { - switch ($column->type) { + foreach ($columns as $column => $type) { + switch ($type) { case Schema::TYPE_SMALLINT: case Schema::TYPE_INTEGER: case Schema::TYPE_BIGINT: @@ -328,10 +348,11 @@ class Generator extends \yii\gii\Generator case Schema::TYPE_TIME: case Schema::TYPE_DATETIME: case Schema::TYPE_TIMESTAMP: - $conditions[] = "\$this->addCondition(\$query, '{$column->name}');"; + case + $conditions[] = "\$this->addCondition(\$query, '{$column}');"; break; default: - $conditions[] = "\$this->addCondition(\$query, '{$column->name}', true);"; + $conditions[] = "\$this->addCondition(\$query, '{$column}', true);"; break; } } @@ -369,10 +390,16 @@ class Generator extends \yii\gii\Generator public function generateActionParamComments() { - $table = $this->getTableSchema(); /** @var ActiveRecord $class */ $class = $this->modelClass; $pks = $class::primaryKey(); + if (($table = $this->getTableSchema()) === false) { + $params = []; + foreach ($pks as $pk) { + $params[] = '@param ' . (substr(strtolower($pk), -2) == 'id' ? 'integer' : 'string') . ' $' . $pk; + } + return $params; + } if (count($pks) === 1) { return ['@param ' . $table->columns[$pks[0]]->phpType . ' $id']; } else { @@ -388,6 +415,23 @@ class Generator extends \yii\gii\Generator { /** @var ActiveRecord $class */ $class = $this->modelClass; - return $class::getTableSchema(); + if (is_subclass_of($class, 'yii\db\ActiveRecord')) { + return $class::getTableSchema(); + } else { + return false; + } } + + public function getColumnNames() + { + /** @var ActiveRecord $class */ + $class = $this->modelClass; + if (is_subclass_of($class, 'yii\db\ActiveRecord')) { + return $class::getTableSchema()->getColumnNames(); + } else { + $model = new $class(); + return $model->attributes(); + } + } + } diff --git a/extensions/gii/generators/crud/templates/controller.php b/extensions/gii/generators/crud/templates/controller.php index 4490cd0..bb5d3f4 100644 --- a/extensions/gii/generators/crud/templates/controller.php +++ b/extensions/gii/generators/crud/templates/controller.php @@ -1,5 +1,6 @@ getTableSchema()->primaryKey; +/** @var ActiveRecordInterface $class */ +$class = $generator->modelClass; +$pks = $class::primaryKey(); $urlParams = $generator->generateUrlParams(); $actionParams = $generator->generateActionParams(); $actionParamComments = $generator->generateActionParamComments(); diff --git a/extensions/gii/generators/crud/templates/views/_form.php b/extensions/gii/generators/crud/templates/views/_form.php index c93ef84..52538d5 100644 --- a/extensions/gii/generators/crud/templates/views/_form.php +++ b/extensions/gii/generators/crud/templates/views/_form.php @@ -12,7 +12,7 @@ use yii\helpers\StringHelper; $model = new $generator->modelClass; $safeAttributes = $model->safeAttributes(); if (empty($safeAttributes)) { - $safeAttributes = $model->getTableSchema()->columnNames; + $safeAttributes = $model->attributes(); } echo "getTableSchema()->getColumnNames() as $attribute) { +foreach ($generator->getColumnNames() as $attribute) { if (++$count < 6) { echo "\t\tgenerateActiveSearchField($attribute) . " ?>\n\n"; } else { diff --git a/extensions/gii/generators/crud/templates/views/index.php b/extensions/gii/generators/crud/templates/views/index.php index b47c3f3..3ad2138 100644 --- a/extensions/gii/generators/crud/templates/views/index.php +++ b/extensions/gii/generators/crud/templates/views/index.php @@ -45,12 +45,22 @@ $this->params['breadcrumbs'][] = $this->title; 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"; +if (($tableSchema = $generator->getTableSchema()) === false) { + foreach ($generator->getColumnNames() as $name) { + if (++$count < 6) { + echo "\t\t\t'" . $name . "',\n"; + } else { + echo "\t\t\t// '" . $name . "',\n"; + } + } +} else { + foreach ($tableSchema->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"; + } } } ?> diff --git a/extensions/gii/generators/crud/templates/views/view.php b/extensions/gii/generators/crud/templates/views/view.php index 9b5391e..9e74aff 100644 --- a/extensions/gii/generators/crud/templates/views/view.php +++ b/extensions/gii/generators/crud/templates/views/view.php @@ -42,9 +42,15 @@ $this->params['breadcrumbs'][] = $this->title; 'model' => $model, 'attributes' => [ getTableSchema()->columns as $column) { - $format = $generator->generateColumnFormat($column); - echo "\t\t\t'" . $column->name . ($format === 'text' ? "" : ":" . $format) . "',\n"; +if (($tableSchema = $generator->getTableSchema()) === false) { + foreach ($generator->getColumnNames() as $name) { + echo "\t\t\t'" . $name . "',\n"; + } +} else { + foreach ($generator->getTableSchema()->columns as $column) { + $format = $generator->generateColumnFormat($column); + echo "\t\t\t'" . $column->name . ($format === 'text' ? "" : ":" . $format) . "',\n"; + } } ?> ],