Commit d650ca3a by Carsten Brandt

updated gii crud to work with non SQL AR

parent 89472955
...@@ -9,6 +9,7 @@ namespace yii\gii\generators\crud; ...@@ -9,6 +9,7 @@ namespace yii\gii\generators\crud;
use Yii; use Yii;
use yii\db\ActiveRecord; use yii\db\ActiveRecord;
use yii\db\BaseActiveRecord;
use yii\db\Schema; use yii\db\Schema;
use yii\gii\CodeFile; use yii\gii\CodeFile;
use yii\helpers\Inflector; use yii\helpers\Inflector;
...@@ -46,7 +47,7 @@ class Generator extends \yii\gii\Generator ...@@ -46,7 +47,7 @@ class Generator extends \yii\gii\Generator
[['modelClass', 'searchModelClass', 'controllerClass', 'baseControllerClass', 'indexWidgetType'], 'required'], [['modelClass', 'searchModelClass', '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' => ActiveRecord::className()]], [['modelClass'], 'validateClass', 'params' => ['extends' => BaseActiveRecord::className()]],
[['baseControllerClass'], 'validateClass', 'params' => ['extends' => Controller::className()]], [['baseControllerClass'], 'validateClass', 'params' => ['extends' => Controller::className()]],
[['controllerClass'], 'match', 'pattern' => '/Controller$/', 'message' => 'Controller class name must be suffixed with "Controller".'], [['controllerClass'], 'match', 'pattern' => '/Controller$/', 'message' => 'Controller class name must be suffixed with "Controller".'],
[['controllerClass', 'searchModelClass'], 'validateNewClass'], [['controllerClass', 'searchModelClass'], 'validateNewClass'],
...@@ -167,13 +168,13 @@ class Generator extends \yii\gii\Generator ...@@ -167,13 +168,13 @@ class Generator extends \yii\gii\Generator
public function getNameAttribute() public function getNameAttribute()
{ {
/** @var \yii\db\ActiveRecord $class */ foreach ($this->getColumnNames() as $name) {
$class = $this->modelClass;
foreach ($class::getTableSchema()->columnNames as $name) {
if (!strcasecmp($name, 'name') || !strcasecmp($name, 'title')) { if (!strcasecmp($name, 'name') || !strcasecmp($name, 'title')) {
return $name; return $name;
} }
} }
/** @var \yii\db\ActiveRecord $class */
$class = $this->modelClass;
$pk = $class::primaryKey(); $pk = $class::primaryKey();
return $pk[0]; return $pk[0];
} }
...@@ -185,9 +186,13 @@ class Generator extends \yii\gii\Generator ...@@ -185,9 +186,13 @@ class Generator extends \yii\gii\Generator
public function generateActiveField($attribute) public function generateActiveField($attribute)
{ {
$tableSchema = $this->getTableSchema(); $tableSchema = $this->getTableSchema();
if (!isset($tableSchema->columns[$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');"; return "\$form->field(\$model, '$attribute');";
} }
}
$column = $tableSchema->columns[$attribute]; $column = $tableSchema->columns[$attribute];
if ($column->phpType === 'boolean') { if ($column->phpType === 'boolean') {
return "\$form->field(\$model, '$attribute')->checkbox()"; return "\$form->field(\$model, '$attribute')->checkbox()";
...@@ -214,6 +219,9 @@ class Generator extends \yii\gii\Generator ...@@ -214,6 +219,9 @@ class Generator extends \yii\gii\Generator
public function generateActiveSearchField($attribute) public function generateActiveSearchField($attribute)
{ {
$tableSchema = $this->getTableSchema(); $tableSchema = $this->getTableSchema();
if ($tableSchema === false) {
return "\$form->field(\$model, '$attribute')";
}
$column = $tableSchema->columns[$attribute]; $column = $tableSchema->columns[$attribute];
if ($column->phpType === 'boolean') { if ($column->phpType === 'boolean') {
return "\$form->field(\$model, '$attribute')->checkbox()"; return "\$form->field(\$model, '$attribute')->checkbox()";
...@@ -249,7 +257,9 @@ class Generator extends \yii\gii\Generator ...@@ -249,7 +257,9 @@ class Generator extends \yii\gii\Generator
*/ */
public function generateSearchRules() public function generateSearchRules()
{ {
$table = $this->getTableSchema(); if (($table = $this->getTableSchema()) === false) {
return ["[['" . implode("', '", $this->getColumnNames()) . "'], 'safe']"];
}
$types = []; $types = [];
foreach ($table->columns as $column) { foreach ($table->columns as $column) {
switch ($column->type) { switch ($column->type) {
...@@ -286,7 +296,7 @@ class Generator extends \yii\gii\Generator ...@@ -286,7 +296,7 @@ class Generator extends \yii\gii\Generator
public function getSearchAttributes() public function getSearchAttributes()
{ {
return $this->getTableSchema()->getColumnNames(); return $this->getColumnNames();
} }
/** /**
...@@ -295,17 +305,16 @@ class Generator extends \yii\gii\Generator ...@@ -295,17 +305,16 @@ class Generator extends \yii\gii\Generator
*/ */
public function generateSearchLabels() public function generateSearchLabels()
{ {
$table = $this->getTableSchema();
$labels = []; $labels = [];
foreach ($table->columns as $column) { foreach ($this->getColumnNames() as $name) {
if (!strcasecmp($column->name, 'id')) { if (!strcasecmp($name, 'id')) {
$labels[$column->name] = 'ID'; $labels[$name] = 'ID';
} else { } else {
$label = Inflector::camel2words($column->name); $label = Inflector::camel2words($name);
if (strcasecmp(substr($label, -3), ' id') === 0) { if (strcasecmp(substr($label, -3), ' id') === 0) {
$label = substr($label, 0, -3) . ' ID'; $label = substr($label, 0, -3) . ' ID';
} }
$labels[$column->name] = $label; $labels[$name] = $label;
} }
} }
return $labels; return $labels;
...@@ -313,10 +322,21 @@ class Generator extends \yii\gii\Generator ...@@ -313,10 +322,21 @@ class Generator extends \yii\gii\Generator
public function generateSearchConditions() public function generateSearchConditions()
{ {
$table = $this->getTableSchema(); $columns = [];
$conditions = []; 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) { foreach ($table->columns as $column) {
switch ($column->type) { $columns[$column->name] = $column->type;
}
}
$conditions = [];
foreach ($columns as $column => $type) {
switch ($type) {
case Schema::TYPE_SMALLINT: case Schema::TYPE_SMALLINT:
case Schema::TYPE_INTEGER: case Schema::TYPE_INTEGER:
case Schema::TYPE_BIGINT: case Schema::TYPE_BIGINT:
...@@ -328,10 +348,11 @@ class Generator extends \yii\gii\Generator ...@@ -328,10 +348,11 @@ class Generator extends \yii\gii\Generator
case Schema::TYPE_TIME: case Schema::TYPE_TIME:
case Schema::TYPE_DATETIME: case Schema::TYPE_DATETIME:
case Schema::TYPE_TIMESTAMP: case Schema::TYPE_TIMESTAMP:
$conditions[] = "\$this->addCondition(\$query, '{$column->name}');"; case
$conditions[] = "\$this->addCondition(\$query, '{$column}');";
break; break;
default: default:
$conditions[] = "\$this->addCondition(\$query, '{$column->name}', true);"; $conditions[] = "\$this->addCondition(\$query, '{$column}', true);";
break; break;
} }
} }
...@@ -369,10 +390,16 @@ class Generator extends \yii\gii\Generator ...@@ -369,10 +390,16 @@ class Generator extends \yii\gii\Generator
public function generateActionParamComments() public function generateActionParamComments()
{ {
$table = $this->getTableSchema();
/** @var ActiveRecord $class */ /** @var ActiveRecord $class */
$class = $this->modelClass; $class = $this->modelClass;
$pks = $class::primaryKey(); $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) { if (count($pks) === 1) {
return ['@param ' . $table->columns[$pks[0]]->phpType . ' $id']; return ['@param ' . $table->columns[$pks[0]]->phpType . ' $id'];
} else { } else {
...@@ -388,6 +415,23 @@ class Generator extends \yii\gii\Generator ...@@ -388,6 +415,23 @@ class Generator extends \yii\gii\Generator
{ {
/** @var ActiveRecord $class */ /** @var ActiveRecord $class */
$class = $this->modelClass; $class = $this->modelClass;
if (is_subclass_of($class, 'yii\db\ActiveRecord')) {
return $class::getTableSchema(); 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();
}
}
} }
<?php <?php
use yii\db\ActiveRecordInterface;
use yii\helpers\StringHelper; use yii\helpers\StringHelper;
/** /**
...@@ -16,7 +17,9 @@ if ($modelClass === $searchModelClass) { ...@@ -16,7 +17,9 @@ if ($modelClass === $searchModelClass) {
$searchModelAlias = $searchModelClass.'Search'; $searchModelAlias = $searchModelClass.'Search';
} }
$pks = $generator->getTableSchema()->primaryKey; /** @var ActiveRecordInterface $class */
$class = $generator->modelClass;
$pks = $class::primaryKey();
$urlParams = $generator->generateUrlParams(); $urlParams = $generator->generateUrlParams();
$actionParams = $generator->generateActionParams(); $actionParams = $generator->generateActionParams();
$actionParamComments = $generator->generateActionParamComments(); $actionParamComments = $generator->generateActionParamComments();
......
...@@ -12,7 +12,7 @@ use yii\helpers\StringHelper; ...@@ -12,7 +12,7 @@ use yii\helpers\StringHelper;
$model = new $generator->modelClass; $model = new $generator->modelClass;
$safeAttributes = $model->safeAttributes(); $safeAttributes = $model->safeAttributes();
if (empty($safeAttributes)) { if (empty($safeAttributes)) {
$safeAttributes = $model->getTableSchema()->columnNames; $safeAttributes = $model->attributes();
} }
echo "<?php\n"; echo "<?php\n";
......
...@@ -30,7 +30,7 @@ use yii\widgets\ActiveForm; ...@@ -30,7 +30,7 @@ use yii\widgets\ActiveForm;
<?php <?php
$count = 0; $count = 0;
foreach ($generator->getTableSchema()->getColumnNames() as $attribute) { foreach ($generator->getColumnNames() as $attribute) {
if (++$count < 6) { if (++$count < 6) {
echo "\t\t<?= " . $generator->generateActiveSearchField($attribute) . " ?>\n\n"; echo "\t\t<?= " . $generator->generateActiveSearchField($attribute) . " ?>\n\n";
} else { } else {
......
...@@ -45,13 +45,23 @@ $this->params['breadcrumbs'][] = $this->title; ...@@ -45,13 +45,23 @@ $this->params['breadcrumbs'][] = $this->title;
<?php <?php
$count = 0; $count = 0;
foreach ($generator->getTableSchema()->columns as $column) { 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); $format = $generator->generateColumnFormat($column);
if (++$count < 6) { if (++$count < 6) {
echo "\t\t\t'" . $column->name . ($format === 'text' ? "" : ":" . $format) . "',\n"; echo "\t\t\t'" . $column->name . ($format === 'text' ? "" : ":" . $format) . "',\n";
} else { } else {
echo "\t\t\t// '" . $column->name . ($format === 'text' ? "" : ":" . $format) . "',\n"; echo "\t\t\t// '" . $column->name . ($format === 'text' ? "" : ":" . $format) . "',\n";
} }
}
} }
?> ?>
......
...@@ -42,9 +42,15 @@ $this->params['breadcrumbs'][] = $this->title; ...@@ -42,9 +42,15 @@ $this->params['breadcrumbs'][] = $this->title;
'model' => $model, 'model' => $model,
'attributes' => [ 'attributes' => [
<?php <?php
foreach ($generator->getTableSchema()->columns as $column) { 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); $format = $generator->generateColumnFormat($column);
echo "\t\t\t'" . $column->name . ($format === 'text' ? "" : ":" . $format) . "',\n"; echo "\t\t\t'" . $column->name . ($format === 'text' ? "" : ":" . $format) . "',\n";
}
} }
?> ?>
], ],
......
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