From bce37252051e7381b7b2585b039d14ba19758fd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sigmar=20K=C3=BChlmann?= Date: Mon, 5 Feb 2024 16:08:37 +0100 Subject: [PATCH 1/2] new commands:./yii faker/clear;./yii faker/refresh --- console/commands/FakerController.php | 34 ++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/console/commands/FakerController.php b/console/commands/FakerController.php index 8cf8b65..7fc9900 100644 --- a/console/commands/FakerController.php +++ b/console/commands/FakerController.php @@ -2,6 +2,8 @@ namespace console\commands; +use Yii; +use yii\base\Model; use yii\console\Controller; use yii\helpers\Console; use yii\helpers\{FileHelper, VarDumper}; @@ -12,6 +14,9 @@ */ class FakerController extends Controller { + /** + * Fill tables with fake data + */ public function actionIndex() { $fakers = FileHelper::findFiles(\Yii::getAlias('@common/models'), [ @@ -36,6 +41,35 @@ public function actionIndex() } } + /** + * Delete all table contents + */ + public function actionClear() + { + $fakers = FileHelper::findFiles(\Yii::getAlias('@common/models'), [ + 'only' => ['*Faker.php'], + 'except' => ['BaseModelFaker.php'], + ]); + + $sortedFakersModels = static::sortModels($fakers, '\\common\\models\\faker\\'); + $sortedFakersModels_DESC = array_reverse($sortedFakersModels); + foreach ($sortedFakersModels_DESC as $modelName) { + /** @var Model $modelClass */ + $modelClass = 'common\\models\\base\\'.$modelName; + Yii::$app->db->createCommand()->delete($modelClass::tableName())->execute(); + $this->stdout("Data from $modelName was deleted\n"); + } + } + + /** + * Delete all table contents and refill with fake data + */ + public function actionRefresh() + { + $this->actionClear(); + $this->actionIndex(); + } + public static function sortModels(array $fakers, string $fakerNamespace = 'app\\models\\') { $modelsDependencies = []; From f2192c9dee21c06c281b8715f7d4e89828653f33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sigmar=20K=C3=BChlmann?= Date: Fri, 31 May 2024 14:12:43 +0200 Subject: [PATCH 2/2] add confirm at actions clear, refresh --- console/commands/FakerController.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/console/commands/FakerController.php b/console/commands/FakerController.php index 7fc9900..2e6e744 100644 --- a/console/commands/FakerController.php +++ b/console/commands/FakerController.php @@ -5,6 +5,7 @@ use Yii; use yii\base\Model; use yii\console\Controller; +use yii\console\ExitCode; use yii\helpers\Console; use yii\helpers\{FileHelper, VarDumper}; use yii\helpers\StringHelper; @@ -44,8 +45,12 @@ public function actionIndex() /** * Delete all table contents */ - public function actionClear() + public function actionClear($requireConfirm = true): int { + if ($requireConfirm && !$this->confirm('Do you really want to delete all data?')) { + return ExitCode::OK; + } + $fakers = FileHelper::findFiles(\Yii::getAlias('@common/models'), [ 'only' => ['*Faker.php'], 'except' => ['BaseModelFaker.php'], @@ -59,15 +64,21 @@ public function actionClear() Yii::$app->db->createCommand()->delete($modelClass::tableName())->execute(); $this->stdout("Data from $modelName was deleted\n"); } + return ExitCode::OK; } /** * Delete all table contents and refill with fake data */ - public function actionRefresh() + public function actionRefresh(): int { - $this->actionClear(); + if (!$this->confirm('Do you really want to delete all data and generate new fake data?')) { + return ExitCode::OK; + } + + $this->actionClear(false); $this->actionIndex(); + return ExitCode::OK; } public static function sortModels(array $fakers, string $fakerNamespace = 'app\\models\\')