Skip to content

Resolve: Default value by constant and expression in CRUD, models, faker etc #65 #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/generator/default/dbmodel.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
*/
abstract class <?= $model->getClassName() ?> extends \yii\db\ActiveRecord
{
<?php if($scenarios = $model->getScenarios()):
foreach($scenarios as $scenario): ?>
<?php if ($scenarios = $model->getScenarios()):
foreach ($scenarios as $scenario): ?>
/**
*<?= $scenario['description'] ?>

Expand All @@ -76,7 +76,7 @@ public static function tableName()
{
return <?= var_export($model->getTableAlias()) ?>;
}
<?php if($scenarios): ?>
<?php if ($scenarios): ?>

/**
* Automatically generated scenarios from the model 'x-scenarios'.
Expand All @@ -92,7 +92,7 @@ public function scenarios()
$default = parent::scenarios()[self::SCENARIO_DEFAULT];

return [
<?php foreach($scenarios as $scenario): ?>
<?php foreach ($scenarios as $scenario): ?>
self::<?= $scenario['const'] ?> => $default,
<?php endforeach; ?>
/**
Expand Down
20 changes: 14 additions & 6 deletions src/lib/ValidationRulesBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
use cebe\yii2openapi\lib\items\Attribute;
use cebe\yii2openapi\lib\items\DbModel;
use cebe\yii2openapi\lib\items\ValidationRule;
use yii\helpers\VarDumper;
use yii\validators\DateValidator;
use yii\db\Expression;
use function count;
use function implode;
use function in_array;
Expand Down Expand Up @@ -192,12 +191,11 @@ private function defaultRule(Attribute $attribute):void
if ($attribute->defaultValue === null) {
return;
}
if ($attribute->defaultValue instanceof \yii\db\Expression) {
return;
}

$params = [];
$params['value'] = $attribute->defaultValue;
$params['value'] = ($attribute->defaultValue instanceof \yii\db\Expression) ?
$this->wrapDefaultExpression($attribute->defaultValue) :
$attribute->defaultValue;
$key = $attribute->columnName . '_default';
$this->rules[$key] = new ValidationRule([$attribute->columnName], 'default', $params);
}
Expand Down Expand Up @@ -251,4 +249,14 @@ private function prepareTypeScope():void
$this->typeScope['safe'][$attribute->columnName] = $attribute->columnName;
}
}

private function wrapDefaultExpression(Expression $dbExpr): Expression
{
return new class($dbExpr->expression) extends Expression {
public function __toString()
{
return '-yii-db-expression-starts-("' . $this->expression . '")-yii-db-expression-ends-';
}
};
}
}
13 changes: 11 additions & 2 deletions src/lib/items/DbModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use yii\base\BaseObject;
use yii\db\ColumnSchema;
use yii\helpers\Inflector;
use yii\helpers\StringHelper;
use yii\helpers\VarDumper;
use function array_filter;
use function array_map;
Expand Down Expand Up @@ -109,7 +108,8 @@ public function getValidationRules():string
$rules = Yii::createObject(ValidationRulesBuilder::class, [$this])->build();
$rules = array_map('strval', $rules);
$rules = VarDumper::export($rules);
return str_replace([

$rules = str_replace([
PHP_EOL,
"\'",
"'[[",
Expand All @@ -120,6 +120,15 @@ public function getValidationRules():string
'[[',
'],'
], $rules);

// https://github.com/php-openapi/yii2-openapi/issues/65
$rules = str_replace(
["'value' => '-yii-db-expression-starts-", "-yii-db-expression-ends-'"],
["'value' => new \yii\db\Expression", ""],
$rules,
);

return $rules;
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/specs/blog/models/base/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function rules()
'flags_integer' => [['flags'], 'integer'],
'flags_default' => [['flags'], 'default', 'value' => 0],
'created_at_datetime' => [['created_at'], 'datetime', 'format' => 'php:Y-m-d H:i:s'],
'created_at_default' => [['created_at'], 'default', 'value' => new \yii\db\Expression("(CURRENT_TIMESTAMP)")],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace app\models;

class Fruit extends \app\models\base\Fruit
{


}

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/**
* This file is generated by Gii, do not change manually!
*/

namespace app\models\base;

/**
* x-db-default-expression test
*
* @property string $ts
* @property string $ts2
* @property string $ts3
* @property string $ts4
* @property string $ts5
* @property string $ts6
* @property string $d
* @property string $d2
* @property string $d3
* @property string $ts7
*
*/
abstract class Fruit extends \yii\db\ActiveRecord
{
public static function tableName()
{
return '{{%fruits}}';
}

public function rules()
{
return [
'trim' => [['ts', 'ts2', 'ts3', 'ts4', 'ts5', 'ts6', 'd', 'd2', 'd3', 'ts7'], 'trim'],
'ts_datetime' => [['ts'], 'datetime', 'format' => 'php:Y-m-d H:i:s'],
'ts_default' => [['ts'], 'default', 'value' => new \yii\db\Expression("(CURRENT_TIMESTAMP)")],
'ts2_datetime' => [['ts2'], 'datetime', 'format' => 'php:Y-m-d H:i:s'],
'ts2_default' => [['ts2'], 'default', 'value' => '2011-11-11 00:00:00'],
'ts3_datetime' => [['ts3'], 'datetime', 'format' => 'php:Y-m-d H:i:s'],
'ts3_default' => [['ts3'], 'default', 'value' => '2022-11-11 00:00:00'],
'ts4_string' => [['ts4'], 'string'],
'ts4_default' => [['ts4'], 'default', 'value' => '2022-11-11 00:00:00'],
'ts5_string' => [['ts5'], 'string'],
'ts5_default' => [['ts5'], 'default', 'value' => new \yii\db\Expression("(CURRENT_TIMESTAMP)")],
'ts6_string' => [['ts6'], 'string'],
'ts6_default' => [['ts6'], 'default', 'value' => '2000-11-11 00:00:00'],
'd_date' => [['d'], 'date', 'format' => 'php:Y-m-d'],
'd_default' => [['d'], 'default', 'value' => new \yii\db\Expression("(CURRENT_DATE + INTERVAL 1 YEAR)")],
'd2_string' => [['d2'], 'string'],
'd2_default' => [['d2'], 'default', 'value' => new \yii\db\Expression("(CURRENT_DATE + INTERVAL 1 YEAR)")],
'd3_string' => [['d3'], 'string'],
'd3_default' => [['d3'], 'default', 'value' => 'text default'],
'ts7_date' => [['ts7'], 'date', 'format' => 'php:Y-m-d'],
'ts7_default' => [['ts7'], 'default', 'value' => new \yii\db\Expression("(CURRENT_DATE + INTERVAL 1 YEAR)")],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace app\models;

class Fruit extends \app\models\base\Fruit
{


}

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/**
* This file is generated by Gii, do not change manually!
*/

namespace app\models\base;

/**
* x-db-default-expression test
*
* @property string $ts
* @property string $ts2
* @property string $ts3
* @property string $ts4
* @property string $ts5
* @property string $ts6
* @property string $d
* @property string $d2
* @property string $d3
* @property string $ts7
*
*/
abstract class Fruit extends \yii\db\ActiveRecord
{
public static function tableName()
{
return '{{%fruits}}';
}

public function rules()
{
return [
'trim' => [['ts', 'ts2', 'ts3', 'ts4', 'ts5', 'ts6', 'd', 'd2', 'd3', 'ts7'], 'trim'],
'ts_datetime' => [['ts'], 'datetime', 'format' => 'php:Y-m-d H:i:s'],
'ts_default' => [['ts'], 'default', 'value' => new \yii\db\Expression("(CURRENT_TIMESTAMP)")],
'ts2_datetime' => [['ts2'], 'datetime', 'format' => 'php:Y-m-d H:i:s'],
'ts2_default' => [['ts2'], 'default', 'value' => '2011-11-11 00:00:00'],
'ts3_datetime' => [['ts3'], 'datetime', 'format' => 'php:Y-m-d H:i:s'],
'ts3_default' => [['ts3'], 'default', 'value' => '2022-11-11 00:00:00'],
'ts4_string' => [['ts4'], 'string'],
'ts4_default' => [['ts4'], 'default', 'value' => '2022-11-11 00:00:00'],
'ts5_string' => [['ts5'], 'string'],
'ts5_default' => [['ts5'], 'default', 'value' => new \yii\db\Expression("(CURRENT_TIMESTAMP)")],
'ts6_string' => [['ts6'], 'string'],
'ts6_default' => [['ts6'], 'default', 'value' => '2000-11-11 00:00:00'],
'd_date' => [['d'], 'date', 'format' => 'php:Y-m-d'],
'd_default' => [['d'], 'default', 'value' => new \yii\db\Expression("(CURRENT_DATE + INTERVAL 1 YEAR)")],
'd2_string' => [['d2'], 'string'],
'd2_default' => [['d2'], 'default', 'value' => new \yii\db\Expression("(CURRENT_DATE + INTERVAL 1 YEAR)")],
'd3_string' => [['d3'], 'string'],
'd3_default' => [['d3'], 'default', 'value' => 'text default'],
'ts7_date' => [['ts7'], 'date', 'format' => 'php:Y-m-d'],
'ts7_default' => [['ts7'], 'default', 'value' => new \yii\db\Expression("(CURRENT_DATE + INTERVAL 1 YEAR)")],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace app\models;

class Fruit extends \app\models\base\Fruit
{


}

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/**
* This file is generated by Gii, do not change manually!
*/

namespace app\models\base;

/**
* x-db-default-expression test
*
* @property string $ts
* @property string $ts2
* @property string $ts3
* @property string $ts4
* @property string $ts5
* @property string $ts6
* @property string $d
* @property string $d2
* @property string $d3
* @property string $ts7
*
*/
abstract class Fruit extends \yii\db\ActiveRecord
{
public static function tableName()
{
return '{{%fruits}}';
}

public function rules()
{
return [
'trim' => [['ts', 'ts2', 'ts3', 'ts4', 'ts5', 'ts6', 'd', 'd2', 'd3', 'ts7'], 'trim'],
'ts_datetime' => [['ts'], 'datetime', 'format' => 'php:Y-m-d H:i:s'],
'ts_default' => [['ts'], 'default', 'value' => new \yii\db\Expression("(CURRENT_TIMESTAMP)")],
'ts2_datetime' => [['ts2'], 'datetime', 'format' => 'php:Y-m-d H:i:s'],
'ts2_default' => [['ts2'], 'default', 'value' => '2011-11-11 00:00:00'],
'ts3_datetime' => [['ts3'], 'datetime', 'format' => 'php:Y-m-d H:i:s'],
'ts3_default' => [['ts3'], 'default', 'value' => '2022-11-11 00:00:00'],
'ts4_string' => [['ts4'], 'string'],
'ts4_default' => [['ts4'], 'default', 'value' => '2022-11-11 00:00:00'],
'ts5_string' => [['ts5'], 'string'],
'ts5_default' => [['ts5'], 'default', 'value' => new \yii\db\Expression("(CURRENT_TIMESTAMP)")],
'ts6_string' => [['ts6'], 'string'],
'ts6_default' => [['ts6'], 'default', 'value' => '2000-11-11 00:00:00'],
'd_date' => [['d'], 'date', 'format' => 'php:Y-m-d'],
'd_default' => [['d'], 'default', 'value' => new \yii\db\Expression("(CURRENT_DATE + INTERVAL 1 YEAR)")],
'd2_string' => [['d2'], 'string'],
'd2_default' => [['d2'], 'default', 'value' => new \yii\db\Expression("(CURRENT_DATE + INTERVAL 1 YEAR)")],
'd3_string' => [['d3'], 'string'],
'd3_default' => [['d3'], 'default', 'value' => 'text default'],
'ts7_date' => [['ts7'], 'date', 'format' => 'php:Y-m-d'],
'ts7_default' => [['ts7'], 'default', 'value' => new \yii\db\Expression("(CURRENT_DATE + INTERVAL 1 YEAR)")],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace app\models;

class Fruit extends \app\models\base\Fruit
{


}

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/**
* This file is generated by Gii, do not change manually!
*/

namespace app\models\base;

/**
* x-db-default-expression test
*
* @property string $ts
* @property string $ts2
* @property string $ts3
* @property string $ts4
* @property string $ts5
* @property string $ts6
* @property string $d
* @property string $d2
* @property string $d3
* @property string $ts7
*
*/
abstract class Fruit extends \yii\db\ActiveRecord
{
public static function tableName()
{
return '{{%fruits}}';
}

public function rules()
{
return [
'trim' => [['ts', 'ts2', 'ts3', 'ts4', 'ts5', 'ts6', 'd', 'd2', 'd3', 'ts7'], 'trim'],
'ts_datetime' => [['ts'], 'datetime', 'format' => 'php:Y-m-d H:i:s'],
'ts_default' => [['ts'], 'default', 'value' => new \yii\db\Expression("(CURRENT_TIMESTAMP)")],
'ts2_datetime' => [['ts2'], 'datetime', 'format' => 'php:Y-m-d H:i:s'],
'ts2_default' => [['ts2'], 'default', 'value' => '2011-11-11 00:00:00'],
'ts3_datetime' => [['ts3'], 'datetime', 'format' => 'php:Y-m-d H:i:s'],
'ts3_default' => [['ts3'], 'default', 'value' => '2022-11-11 00:00:00'],
'ts4_string' => [['ts4'], 'string'],
'ts4_default' => [['ts4'], 'default', 'value' => '2022-11-11 00:00:00'],
'ts5_string' => [['ts5'], 'string'],
'ts5_default' => [['ts5'], 'default', 'value' => new \yii\db\Expression("(CURRENT_TIMESTAMP)")],
'ts6_string' => [['ts6'], 'string'],
'ts6_default' => [['ts6'], 'default', 'value' => '2000-11-11 00:00:00'],
'd_date' => [['d'], 'date', 'format' => 'php:Y-m-d'],
'd_default' => [['d'], 'default', 'value' => new \yii\db\Expression("(CURRENT_DATE + INTERVAL 1 YEAR)")],
'd2_string' => [['d2'], 'string'],
'd2_default' => [['d2'], 'default', 'value' => new \yii\db\Expression("(CURRENT_DATE + INTERVAL 1 YEAR)")],
'd3_string' => [['d3'], 'string'],
'd3_default' => [['d3'], 'default', 'value' => 'text default'],
'ts7_date' => [['ts7'], 'date', 'format' => 'php:Y-m-d'],
'ts7_default' => [['ts7'], 'default', 'value' => new \yii\db\Expression("(CURRENT_DATE + INTERVAL 1 YEAR)")],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace app\models;

class Fruit extends \app\models\base\Fruit
{


}

Loading
Loading