Skip to content

Commit

Permalink
Merge pull request #1 from YetOpen/main
Browse files Browse the repository at this point in the history
Changes to Seeder creation and faker field generation for created_at and updated_at
  • Loading branch information
sugeng-sulistiyawan authored May 23, 2024
2 parents 5f750c8 + 2262561 commit b02b564
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ phpunit.phar
.php-cs-fixer.cache

/.vscode
/.idea
12 changes: 7 additions & 5 deletions src/SeederController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use yii\console\Controller;
use yii\console\ExitCode;
use yii\db\ColumnSchema;
use yii\db\Schema;
use yii\helpers\ArrayHelper;
use yii\helpers\Console;
use yii\helpers\FileHelper;
Expand Down Expand Up @@ -278,7 +279,7 @@ protected function generateFields()
$ref_table_id = $foreign->tableSchema->primaryKey[0];
}

$faker = $this->generateFakerField($data->name) ?? $this->generateFakerField($data->type);
$faker = $this->generateFakerField($data->name, $data->type) ?? $this->generateFakerField($data->type);
if ($data->dbType === 'tinyint(1)') {
$faker = 'boolean';
}
Expand All @@ -297,9 +298,10 @@ protected function generateFields()
* Generate Faker Field Name
*
* @param string $key
* @param string|null $dataType The data type of the column schema
* @return string
*/
protected function generateFakerField($key)
protected function generateFakerField($key, $dataType = null)
{
$faker = [
'full_name' => 'name',
Expand All @@ -316,8 +318,8 @@ protected function generateFakerField($key)
'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")',
'created_at' => $dataType === Schema::TYPE_INTEGER ? 'dateTime()->getTimestamp()' : 'dateTime()->format("Y-m-d H:i:s")',
'updated_at' => $dataType === Schema::TYPE_INTEGER ? 'dateTime()->getTimestamp()' : 'dateTime()->format("Y-m-d H:i:s")',
'token' => 'uuid',
'duration' => 'numberBetween()',

Expand Down Expand Up @@ -345,7 +347,7 @@ protected function generateFakerField($key)
protected function getDefaultSeeder()
{
$defaultSeederClass = "{$this->seederNamespace}\\{$this->defaultSeederClass}";
$defaultSeederFile = str_replace('\\', DIRECTORY_SEPARATOR, "{$defaultSeederClass}.php");
$defaultSeederFile = "{$this->seederPath}/{$defaultSeederClass}.php";

if (!class_exists($defaultSeederClass) || !file_exists($defaultSeederFile)) {
FileHelper::createDirectory($this->seederPath);
Expand Down

0 comments on commit b02b564

Please sign in to comment.