forked from cebe/yii2-openapi
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
be59c82
commit 65fa9b1
Showing
10 changed files
with
367 additions
and
7 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
...n_name_without_underscore/mysql/migrations_mysql_db/m200000_000000_create_table_users.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
/** | ||
* Table for User | ||
*/ | ||
class m200000_000000_create_table_users extends \yii\db\Migration | ||
{ | ||
public function up() | ||
{ | ||
$this->createTable('{{%users}}', [ | ||
'id' => $this->primaryKey(), | ||
'name' => $this->text()->null(), | ||
]); | ||
} | ||
|
||
public function down() | ||
{ | ||
$this->dropTable('{{%users}}'); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...n_name_without_underscore/mysql/migrations_mysql_db/m200000_000001_create_table_posts.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
/** | ||
* Table for Post | ||
*/ | ||
class m200000_000001_create_table_posts extends \yii\db\Migration | ||
{ | ||
public function up() | ||
{ | ||
$this->createTable('{{%posts}}', [ | ||
'id' => $this->primaryKey(), | ||
'content' => $this->text()->null(), | ||
'user' => $this->integer()->null()->defaultValue(null), | ||
]); | ||
$this->addForeignKey('fk_posts_user_users_id', '{{%posts}}', 'user', '{{%users}}', 'id'); | ||
} | ||
|
||
public function down() | ||
{ | ||
$this->dropForeignKey('fk_posts_user_users_id', '{{%posts}}'); | ||
$this->dropTable('{{%posts}}'); | ||
} | ||
} |
144 changes: 144 additions & 0 deletions
144
...ame_cause_error_in_case_of_column_name_without_underscore/mysql/models/BaseModelFaker.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
<?php | ||
|
||
namespace app\models; | ||
|
||
use Faker\Factory as FakerFactory; | ||
use Faker\Generator; | ||
use Faker\UniqueGenerator; | ||
|
||
/** | ||
* Base fake data generator | ||
*/ | ||
abstract class BaseModelFaker | ||
{ | ||
/** | ||
* @var Generator | ||
*/ | ||
protected $faker; | ||
/** | ||
* @var UniqueGenerator | ||
*/ | ||
protected $uniqueFaker; | ||
|
||
public function __construct() | ||
{ | ||
$this->faker = FakerFactory::create(str_replace('-', '_', \Yii::$app->language)); | ||
$this->uniqueFaker = new UniqueGenerator($this->faker); | ||
} | ||
|
||
abstract public function generateModel($attributes = []); | ||
|
||
public function getFaker():Generator | ||
{ | ||
return $this->faker; | ||
} | ||
|
||
public function getUniqueFaker():UniqueGenerator | ||
{ | ||
return $this->uniqueFaker; | ||
} | ||
|
||
public function setFaker(Generator $faker):void | ||
{ | ||
$this->faker = $faker; | ||
} | ||
|
||
public function setUniqueFaker(UniqueGenerator $faker):void | ||
{ | ||
$this->uniqueFaker = $faker; | ||
} | ||
|
||
/** | ||
* Generate and return model | ||
* @param array|callable $attributes | ||
* @param UniqueGenerator|null $uniqueFaker | ||
* @return \yii\db\ActiveRecord | ||
* @example MyFaker::makeOne(['user_id' => 1, 'title' => 'foo']); | ||
* @example MyFaker::makeOne( function($model, $faker) { | ||
* $model->scenario = 'create'; | ||
* $model->setAttributes(['user_id' => 1, 'title' => $faker->sentence]); | ||
* return $model; | ||
* }); | ||
*/ | ||
public static function makeOne($attributes = [], ?UniqueGenerator $uniqueFaker = null) | ||
{ | ||
$fakeBuilder = new static(); | ||
if ($uniqueFaker !== null) { | ||
$fakeBuilder->setUniqueFaker($uniqueFaker); | ||
} | ||
$model = $fakeBuilder->generateModel($attributes); | ||
return $model; | ||
} | ||
|
||
/** | ||
* Generate, save and return model | ||
* @param array|callable $attributes | ||
* @param UniqueGenerator|null $uniqueFaker | ||
* @return \yii\db\ActiveRecord | ||
* @example MyFaker::saveOne(['user_id' => 1, 'title' => 'foo']); | ||
* @example MyFaker::saveOne( function($model, $faker) { | ||
* $model->scenario = 'create'; | ||
* $model->setAttributes(['user_id' => 1, 'title' => $faker->sentence]); | ||
* return $model; | ||
* }); | ||
*/ | ||
public static function saveOne($attributes = [], ?UniqueGenerator $uniqueFaker = null) | ||
{ | ||
$model = static::makeOne($attributes, $uniqueFaker); | ||
$model->save(); | ||
return $model; | ||
} | ||
|
||
/** | ||
* Generate and return multiple models | ||
* @param int $number | ||
* @param array|callable $commonAttributes | ||
* @return \yii\db\ActiveRecord[]|array | ||
* @example TaskFaker::make(5, ['project_id'=>1, 'user_id' => 2]); | ||
* @example TaskFaker::make(5, function($model, $faker, $uniqueFaker) { | ||
* $model->setAttributes(['name' => $uniqueFaker->username, 'state'=>$faker->boolean(20)]); | ||
* return $model; | ||
* }); | ||
*/ | ||
public static function make(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null):array | ||
{ | ||
if ($number < 1) { | ||
return []; | ||
} | ||
$fakeBuilder = new static(); | ||
if ($uniqueFaker !== null) { | ||
$fakeBuilder->setUniqueFaker($uniqueFaker); | ||
} | ||
return array_map(function () use ($commonAttributes, $fakeBuilder) { | ||
$model = $fakeBuilder->generateModel($commonAttributes); | ||
return $model; | ||
}, range(0, $number -1)); | ||
} | ||
|
||
/** | ||
* Generate, save and return multiple models | ||
* @param int $number | ||
* @param array|callable $commonAttributes | ||
* @return \yii\db\ActiveRecord[]|array | ||
* @example TaskFaker::save(5, ['project_id'=>1, 'user_id' => 2]); | ||
* @example TaskFaker::save(5, function($model, $faker, $uniqueFaker) { | ||
* $model->setAttributes(['name' => $uniqueFaker->username, 'state'=>$faker->boolean(20)]); | ||
* return $model; | ||
* }); | ||
*/ | ||
public static function save(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null):array | ||
{ | ||
if ($number < 1) { | ||
return []; | ||
} | ||
$fakeBuilder = new static(); | ||
if ($uniqueFaker !== null) { | ||
$fakeBuilder->setUniqueFaker($uniqueFaker); | ||
} | ||
return array_map(function () use ($commonAttributes, $fakeBuilder) { | ||
$model = $fakeBuilder->generateModel($commonAttributes); | ||
$model->save(); | ||
return $model; | ||
}, range(0, $number -1)); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...k_column_name_cause_error_in_case_of_column_name_without_underscore/mysql/models/Post.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace app\models; | ||
|
||
class Post extends \app\models\base\Post | ||
{ | ||
|
||
|
||
} | ||
|
51 changes: 51 additions & 0 deletions
51
...umn_name_cause_error_in_case_of_column_name_without_underscore/mysql/models/PostFaker.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
namespace app\models; | ||
|
||
use Faker\UniqueGenerator; | ||
|
||
/** | ||
* Fake data generator for Post | ||
* @method static Post makeOne($attributes = [], ?UniqueGenerator $uniqueFaker = null); | ||
* @method static Post saveOne($attributes = [], ?UniqueGenerator $uniqueFaker = null); | ||
* @method static Post[] make(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null) | ||
* @method static Post[] save(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null) | ||
*/ | ||
class PostFaker extends BaseModelFaker | ||
{ | ||
|
||
/** | ||
* @param array|callable $attributes | ||
* @return Post|\yii\db\ActiveRecord | ||
* @example | ||
* $model = (new PostFaker())->generateModels(['author_id' => 1]); | ||
* $model = (new PostFaker())->generateModels(function($model, $faker, $uniqueFaker) { | ||
* $model->scenario = 'create'; | ||
* $model->author_id = 1; | ||
* return $model; | ||
* }); | ||
**/ | ||
public function generateModel($attributes = []) | ||
{ | ||
$faker = $this->faker; | ||
$uniqueFaker = $this->uniqueFaker; | ||
$model = new Post(); | ||
//$model->id = $uniqueFaker->numberBetween(0, 1000000); | ||
$model->content = $faker->paragraphs(6, true); | ||
$model->user = $faker->randomElement(\app\models\User::find()->select("id")->column()); | ||
if (!is_callable($attributes)) { | ||
$model->setAttributes($attributes, false); | ||
} else { | ||
$model = $attributes($model, $faker, $uniqueFaker); | ||
} | ||
return $model; | ||
} | ||
|
||
public static function dependentOn() | ||
{ | ||
return [ | ||
// just model class names | ||
'User', | ||
|
||
]; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...k_column_name_cause_error_in_case_of_column_name_without_underscore/mysql/models/User.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace app\models; | ||
|
||
class User extends \app\models\base\User | ||
{ | ||
|
||
|
||
} | ||
|
41 changes: 41 additions & 0 deletions
41
...umn_name_cause_error_in_case_of_column_name_without_underscore/mysql/models/UserFaker.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
namespace app\models; | ||
|
||
use Faker\UniqueGenerator; | ||
|
||
/** | ||
* Fake data generator for User | ||
* @method static User makeOne($attributes = [], ?UniqueGenerator $uniqueFaker = null); | ||
* @method static User saveOne($attributes = [], ?UniqueGenerator $uniqueFaker = null); | ||
* @method static User[] make(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null) | ||
* @method static User[] save(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null) | ||
*/ | ||
class UserFaker extends BaseModelFaker | ||
{ | ||
|
||
/** | ||
* @param array|callable $attributes | ||
* @return User|\yii\db\ActiveRecord | ||
* @example | ||
* $model = (new PostFaker())->generateModels(['author_id' => 1]); | ||
* $model = (new PostFaker())->generateModels(function($model, $faker, $uniqueFaker) { | ||
* $model->scenario = 'create'; | ||
* $model->author_id = 1; | ||
* return $model; | ||
* }); | ||
**/ | ||
public function generateModel($attributes = []) | ||
{ | ||
$faker = $this->faker; | ||
$uniqueFaker = $this->uniqueFaker; | ||
$model = new User(); | ||
//$model->id = $uniqueFaker->numberBetween(0, 1000000); | ||
$model->name = $faker->sentence; | ||
if (!is_callable($attributes)) { | ||
$model->setAttributes($attributes, false); | ||
} else { | ||
$model = $attributes($model, $faker, $uniqueFaker); | ||
} | ||
return $model; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...umn_name_cause_error_in_case_of_column_name_without_underscore/mysql/models/base/Post.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace app\models\base; | ||
|
||
/** | ||
* | ||
* | ||
* @property int $id | ||
* @property string $content | ||
* @property int $user | ||
* | ||
* @property \app\models\User $userRel | ||
*/ | ||
abstract class Post extends \yii\db\ActiveRecord | ||
{ | ||
public static function tableName() | ||
{ | ||
return '{{%posts}}'; | ||
} | ||
|
||
public function rules() | ||
{ | ||
return [ | ||
'trim' => [['content'], 'trim'], | ||
'user_integer' => [['user'], 'integer'], | ||
'user_exist' => [['user'], 'exist', 'targetRelation' => 'userRel'], | ||
'content_string' => [['content'], 'string'], | ||
]; | ||
} | ||
|
||
public function getUserRel() | ||
{ | ||
return $this->hasOne(\app\models\User::class, ['id' => 'user']); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...umn_name_cause_error_in_case_of_column_name_without_underscore/mysql/models/base/User.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace app\models\base; | ||
|
||
/** | ||
* | ||
* | ||
* @property int $id | ||
* @property string $name | ||
* | ||
*/ | ||
abstract class User extends \yii\db\ActiveRecord | ||
{ | ||
public static function tableName() | ||
{ | ||
return '{{%users}}'; | ||
} | ||
|
||
public function rules() | ||
{ | ||
return [ | ||
'trim' => [['name'], 'trim'], | ||
'name_string' => [['name'], 'string'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters