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;
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,9 +186,13 @@ class Generator extends \yii\gii\Generator
public function generateActiveField($attribute)
{
$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');";
}
}
$column = $tableSchema->columns[$attribute];
if ($column->phpType === 'boolean') {
return "\$form->field(\$model, '$attribute')->checkbox()";
......@@ -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();
$conditions = [];
$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) {
switch ($column->type) {
$columns[$column->name] = $column->type;
}
}
$conditions = [];
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;
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();
}
}
}
<?php
use yii\db\ActiveRecordInterface;
use yii\helpers\StringHelper;
/**
......@@ -16,7 +17,9 @@ if ($modelClass === $searchModelClass) {
$searchModelAlias = $searchModelClass.'Search';
}
$pks = $generator->getTableSchema()->primaryKey;
/** @var ActiveRecordInterface $class */
$class = $generator->modelClass;
$pks = $class::primaryKey();
$urlParams = $generator->generateUrlParams();
$actionParams = $generator->generateActionParams();
$actionParamComments = $generator->generateActionParamComments();
......
......@@ -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 "<?php\n";
......
......@@ -30,7 +30,7 @@ use yii\widgets\ActiveForm;
<?php
$count = 0;
foreach ($generator->getTableSchema()->getColumnNames() as $attribute) {
foreach ($generator->getColumnNames() as $attribute) {
if (++$count < 6) {
echo "\t\t<?= " . $generator->generateActiveSearchField($attribute) . " ?>\n\n";
} else {
......
......@@ -45,13 +45,23 @@ $this->params['breadcrumbs'][] = $this->title;
<?php
$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);
if (++$count < 6) {
echo "\t\t\t'" . $column->name . ($format === 'text' ? "" : ":" . $format) . "',\n";
} else {
echo "\t\t\t// '" . $column->name . ($format === 'text' ? "" : ":" . $format) . "',\n";
}
}
}
?>
......
......@@ -42,9 +42,15 @@ $this->params['breadcrumbs'][] = $this->title;
'model' => $model,
'attributes' => [
<?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);
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