@ -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
[['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' => Base ActiveRecord::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
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
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
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
*/
*/
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
public function getSearchAttributes()
public function getSearchAttributes()
{
{
return $this->getTableSchema()->get ColumnNames();
return $this->getColumnNames();
}
}
/**
/**
@ -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
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
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
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
{
{
/** @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();
}
}
}
}