diff --git a/README.md b/README.md index 77bba27..a38fb8b 100644 --- a/README.md +++ b/README.md @@ -211,11 +211,16 @@ public $locale = 'en_US'; At the end of every Seeder, if any columns have been forgotten, a message with all the missing columns will appear ```shell - > #################### MISSING COLUMNS ################### - > # TABLE: {{%user}} # - > # name => varchar(255) # - > # age => int(2) # - > ######################################################## + > ########################### MISSING COLUMNS ############################ + > # # + > # TABLE: {{%user}} # + > # -------------------------------------------------------------------- # + > # - full_name => varchar(1) # + > # - birth_date => date # + > # - thumbnail => varchar(64) # + > # -------------------------------------------------------------------- # + > # # + > ######################################################################## ``` ### Seeder diff --git a/src/SeederController.php b/src/SeederController.php index 64eae60..7f09c40 100644 --- a/src/SeederController.php +++ b/src/SeederController.php @@ -99,8 +99,20 @@ public function actionSeed($name = "") $function = $explode[1] ?? null; if ($name) { - $func = $function ?? 'run'; + $modelClass = str_replace('/', '\\', $name); + $explode = explode('\\', $modelClass); + $modelName = Inflector::camelize(array_pop($explode)); + $modelNamespace = implode('\\', $explode); + + $modelClass = $modelNamespace ? "{$modelNamespace}\\{$modelName}" : $modelName; + $func = $function ?? 'run'; $seederClasses = [ + $modelClass, + "{$modelClass}TableSeeder", + "{$this->seederNamespace}\\{$modelClass}", + "{$this->seederNamespace}\\{$modelClass}TableSeeder", + "{$this->tableSeederNamespace}\\{$modelClass}", + "{$this->tableSeederNamespace}\\{$modelClass}TableSeeder", $name, "{$name}TableSeeder", "{$this->seederNamespace}\\{$name}", @@ -211,7 +223,7 @@ public function actionCreate($modelName) ]); FileHelper::createDirectory($this->tablesPath); - if (!file_exists($file) || $this->confirm("\n'{$className}' already exists, overwrite?\nAll data will be lost irreversibly!")) { + if (!file_exists($file) || $this->confirm("\n'{$file}' already exists, overwrite?\nAll data will be lost irreversibly!")) { file_put_contents($file, $content, LOCK_EX); $this->stdout("New seeder created successfully.\n", Console::FG_GREEN); } @@ -319,20 +331,27 @@ protected function generateFakerField($key) 'email' => 'email', 'phone' => 'phoneNumber', 'hp' => 'phoneNumber', + 'start_date' => 'dateTime()->format("Y-m-d H:i:s")', + 'end_date' => 'dateTime()->format("Y-m-d H:i:s")', + 'created_at' => 'dateTime()->format("Y-m-d H:i:s")', + 'updated_at' => 'dateTime()->format("Y-m-d H:i:s")', + 'token' => 'uuid', + 'duration' => 'numberBetween()', + 'integer' => 'numberBetween(0, 10)', 'smallint' => 'numberBetween(0, 10)', 'tinyint' => 'numberBetween(0, 10)', 'mediumint' => 'numberBetween(0, 10)', 'int' => 'numberBetween(0, 10)', - 'bigint' => 'numberBetween(0, 10)', + 'bigint' => 'numberBetween()', 'date' => 'date()', - 'datetime' => 'dateTime()', - 'timestamp' => 'dateTime()', + 'datetime' => 'dateTime()->format("Y-m-d H:i:s")', + 'timestamp' => 'dateTime()->format("Y-m-d H:i:s")', 'year' => 'year()', 'time' => 'time()', ]; - return ArrayHelper::getValue($faker, $key, 'text'); + return ArrayHelper::getValue($faker, $key, 'word'); } /** diff --git a/src/TableSeeder.php b/src/TableSeeder.php index ffa088d..7d7af61 100644 --- a/src/TableSeeder.php +++ b/src/TableSeeder.php @@ -157,14 +157,18 @@ protected function checkMissingColumns($insertedColumns) } if (count($missingColumns)) { - echo " > " . str_pad(' MISSING COLUMNS ', 70, '#', STR_PAD_BOTH) . "\n"; + echo " > #" . str_pad(' MISSING COLUMNS ', 70, '#', STR_PAD_BOTH) . "#\n"; foreach ($missingColumns as $table => $columns) { - echo " > " . str_pad("# TABLE: $table", 69, ' ') . "#\n"; + echo " > #" . str_pad('', 70, ' ') . "#\n"; + echo " > #" . str_pad(" TABLE: {$table}", 70, ' ') . "#\n"; + echo " > # " . str_pad('', 68, '-') . " #\n"; foreach ($columns as [$tableColumn, $type]) { - echo " > " . str_pad("# $tableColumn => $type", 69, ' ') . "#\n"; + echo " > #" . str_pad(" - $tableColumn => $type", 70, ' ') . "#\n"; } + echo " > # " . str_pad('', 68, '-') . " #\n"; + echo " > #" . str_pad('', 70, ' ') . "#\n"; } - echo " > " . str_pad('', 70, '#') . "\n"; + echo " > #" . str_pad('', 70, '#') . "#\n"; } } diff --git a/src/views/TableSeeder.php b/src/views/TableSeeder.php index 5325f8d..b88ac2f 100644 --- a/src/views/TableSeeder.php +++ b/src/views/TableSeeder.php @@ -1,4 +1,5 @@ use diecoding\seeder\TableSeeder; - $properties) { if ($foreign = $properties->foreign) { - $_class = $foreign::class; - $_className = StringHelper::basename($_class); - if (isset($check[$_className])) { - $_className = $_className . count($check[$_className]); + $foreignClass = $foreign::class; + $foreignClassName = StringHelper::basename($foreignClass); + if (!isset($checkClass[$foreignClass])) { + if (isset($checkClassName[$foreignClassName])) { + $foreignClassName .= count($checkClassName[$foreignClassName]); + $checkClassName[$foreignClassName][] = $foreignClass; - echo "use {$_class} as {$_className};\n"; - } else { - echo "use {$_class};\n"; + echo "use {$foreignClass} as {$foreignClassName};\n"; + } else { + $checkClassName[$foreignClassName] = [$foreignClass]; + + echo "use {$foreignClass};\n"; + } + $checkClass[$foreignClass] = $foreignClass; } - $check[$_className] = [$_class]; - $ref_table_id = $properties->ref_table_id; - $space = $i++ === 0 ? '' : "\t\t"; - $vars[$ref_table_id . $column] = '$' . Inflector::variablize($_className); - $varsPre[$ref_table_id . $column] = "{$space}{$vars[$ref_table_id . $column]} = {$_className}::find()->all();\n"; + $ref_table_id = $properties->ref_table_id; + $vars[$ref_table_id . $column] = '$' . Inflector::variablize($foreignClassName); + $foreignVars[$foreignClassName] = "{$vars[$ref_table_id .$column]} = {$foreignClassName}::find()->all();\n"; } } ?> @@ -60,26 +67,26 @@ class extends TableSeeder /** * {@inheritdoc} */ - function run() + public function run() { - + $count = 100; - for ($i = 0; $i < $count; $i++) { + for ($i = 0; $i < $count; $i++) { $this->insert(::tableName(), [ $properties) { - $ref_table_id = $properties->ref_table_id; - $space = $i++ === 0 ? '' : "\t\t\t\t"; - if (isset($vars[$ref_table_id . $column])) { - echo $space . "'$column' => \$this->faker->randomElement({$vars[$ref_table_id . $column]})->{$properties->ref_table_id},\n"; - } else { - echo $space . "'$column' => \$this->faker->{$properties->faker},\n"; - } - } + $i = 0; + foreach ($fields as $column => $properties) { + $ref_table_id = $properties->ref_table_id; + $space = $i++ === 0 ? '' : "\t\t\t\t"; + if (isset($vars[$ref_table_id . $column])) { + echo $space . "'$column' => \$this->faker->randomElement({$vars[$ref_table_id .$column]})->{$properties->ref_table_id},\n"; + } else { + echo $space . "'$column' => \$this->faker->{$properties->faker},\n"; + } + } ?> ]); } } -} +} \ No newline at end of file