Skip to content

Commit bce3725

Browse files
committed
new commands:./yii faker/clear;./yii faker/refresh
1 parent 2994bb3 commit bce3725

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

console/commands/FakerController.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace console\commands;
44

5+
use Yii;
6+
use yii\base\Model;
57
use yii\console\Controller;
68
use yii\helpers\Console;
79
use yii\helpers\{FileHelper, VarDumper};
@@ -12,6 +14,9 @@
1214
*/
1315
class FakerController extends Controller
1416
{
17+
/**
18+
* Fill tables with fake data
19+
*/
1520
public function actionIndex()
1621
{
1722
$fakers = FileHelper::findFiles(\Yii::getAlias('@common/models'), [
@@ -36,6 +41,35 @@ public function actionIndex()
3641
}
3742
}
3843

44+
/**
45+
* Delete all table contents
46+
*/
47+
public function actionClear()
48+
{
49+
$fakers = FileHelper::findFiles(\Yii::getAlias('@common/models'), [
50+
'only' => ['*Faker.php'],
51+
'except' => ['BaseModelFaker.php'],
52+
]);
53+
54+
$sortedFakersModels = static::sortModels($fakers, '\\common\\models\\faker\\');
55+
$sortedFakersModels_DESC = array_reverse($sortedFakersModels);
56+
foreach ($sortedFakersModels_DESC as $modelName) {
57+
/** @var Model $modelClass */
58+
$modelClass = 'common\\models\\base\\'.$modelName;
59+
Yii::$app->db->createCommand()->delete($modelClass::tableName())->execute();
60+
$this->stdout("Data from $modelName was deleted\n");
61+
}
62+
}
63+
64+
/**
65+
* Delete all table contents and refill with fake data
66+
*/
67+
public function actionRefresh()
68+
{
69+
$this->actionClear();
70+
$this->actionIndex();
71+
}
72+
3973
public static function sortModels(array $fakers, string $fakerNamespace = 'app\\models\\')
4074
{
4175
$modelsDependencies = [];

0 commit comments

Comments
 (0)