Mercurial > packages > magicforger
changeset 40:2cf26b593f4a ls_dev_2025_09 tip
better support for different column types
| author | Luka Sitas <sitas.luka.97@gmail.com> |
|---|---|
| date | Thu, 16 Oct 2025 10:54:04 -0400 |
| parents | b5c6ebd33547 |
| children | |
| files | src/Generator/Factory/FactoryGenerator.php src/Generator/Model/stubs/model.stub src/Generator/Requests/RequestGenerator.php src/Generator/Test/stubs/test.stub src/Replacer/TableReplacer.php |
| diffstat | 5 files changed, 43 insertions(+), 54 deletions(-) [+] |
line wrap: on
line diff
--- a/src/Generator/Factory/FactoryGenerator.php Thu Sep 25 23:16:13 2025 -0400 +++ b/src/Generator/Factory/FactoryGenerator.php Thu Oct 16 10:54:04 2025 -0400 @@ -85,71 +85,58 @@ protected function gatherRelations() { $relations = RelationshipNavigator::getRelations($this->getCurrentTable()); + foreach ($relations as $relation_type => $relation_values) { + $relations[$relation_type] = array_column($relation_values, null, 'column'); + } return $relations; - } protected function renderColumns() { - $insert = ''; $values = []; + $relations = $this->gatherRelations(); + foreach ($this->get_columns() as $column) { if (in_array($column['name'], $this->columns_to_ignore)) { continue; } - $type = $column['type_name']; - $nullable = ($column['nullable'] ? '->optional($weight = 0.5)' : ''); - $value = 'fake()'.$nullable; - $name = $column['name']; - - // Get the expected header name - $replacements = [ - '{{value}}' => $value.'->text()', - '{{column_name}}' => $name, - ]; - - // date - if (in_array($type, ['date'])) { - $replacements['{{value}}'] = $value.'->date()'; - } - // time - if (in_array($type, ['timestamp'])) { - $replacements['{{value}}'] = $value.'->timestamp()'; - } - // checkbox - if (in_array($type, ['tinyint'])) { - $replacements['{{value}}'] = $value.'->numberBetween(0,1)'; - } - // select - elseif (in_array($type, ['bigint']) && array_key_exists($name, $selects)) { - $replacements['{{header}}'] = Str::headline(Str::singular($selects[$name])); - $replacements['{{value}}'] = '{{ $item->'.Str::singular($selects[$name]).'?->name ?? "" }}'; - } - // bigint, float - elseif (in_array($type, ['bigint', 'int'])) { - $replacements['{{value}}'] = $value.'->randomNumber()'; - } elseif (in_array($type, ['float'])) { - $replacements['{{value}}'] = $value.'->randomFloat()'; - } else { - // text area - // varchar, , etc - } - + $value = $this->getFakeValue($column, $relations); $snippet = $this->getSnippet('value'); - // Replace placeholders with actual values - $values[] = str_replace( - array_keys($replacements), - $replacements, - $snippet - ); // Replace placeholders with actual values + $values[] = str_replace(['{{value}}', '{{column_name}}'], [$value, $column['name']], $snippet); } - $insert = implode("\n", $values); + + return implode("\n", $values); + } + + protected function getFakeValue($column, $relations) + { + $value = '$this->faker'; + $nullable = ($column['nullable'] ? '->optional($weight = 0.5)' : ''); - return $insert; + switch ($column['type_name']) { + case 'date': + return "$value$nullable->date()"; + case 'timestamp': + return "$value$nullable->timestamp()"; + case 'tinyint': + return "$value$nullable->numberBetween(0,1)"; + case 'bigint': + if (isset($relations['belongsTo'][$column['name']])) { + $related = $this->getNamespacedModel($relations['belongsTo'][$column['name']]['table']); + return "$related::factory()"; + } + // fall through to int for lack of better option + case 'int': + return "$value$nullable->randomNumber()"; + case 'float': + return "$value$nullable->randomFloat()"; + default: + return "$value$nullable->text()"; + } } /**
--- a/src/Generator/Model/stubs/model.stub Thu Sep 25 23:16:13 2025 -0400 +++ b/src/Generator/Model/stubs/model.stub Thu Oct 16 10:54:04 2025 -0400 @@ -2,13 +2,14 @@ namespace {{ namespace }}; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\SoftDeletes; use Wizard\Framework\Models\BaseModel; class {{ class }} extends BaseModel { /** @use HasFactory<\Database\Factories\{{ class }}Factory> */ - //use HasFactory; + use HasFactory; use SoftDeletes; /**
--- a/src/Generator/Requests/RequestGenerator.php Thu Sep 25 23:16:13 2025 -0400 +++ b/src/Generator/Requests/RequestGenerator.php Thu Oct 16 10:54:04 2025 -0400 @@ -90,16 +90,16 @@ protected function createFilterRequest() { - $this->call('mf:filter_request', ['table' => $this->getTableInput()]); + $this->call('mf:filter_request', ['table' => $this->getTableInput(), '--fresh' => $this->option('fresh')]); } protected function createStoreRequest() { - $this->call('mf:store_request', ['table' => $this->getTableInput()]); + $this->call('mf:store_request', ['table' => $this->getTableInput(), '--fresh' => $this->option('fresh')]); } protected function createUpdateRequest() { - $this->call('mf:update_request', ['table' => $this->getTableInput()]); + $this->call('mf:update_request', ['table' => $this->getTableInput(), '--fresh' => $this->option('fresh')]); } }
--- a/src/Generator/Test/stubs/test.stub Thu Sep 25 23:16:13 2025 -0400 +++ b/src/Generator/Test/stubs/test.stub Thu Oct 16 10:54:04 2025 -0400 @@ -1,8 +1,9 @@ <?php -namespace Tests\CRUD\{{ model }}; +namespace Tests\CRUD; use {{ namespacedModel }}; +use Illuminate\Database\Eloquent\Factories\HasFactory; use App\Models\User; use Wizard\Framework\Tests\TestCase;
--- a/src/Replacer/TableReplacer.php Thu Sep 25 23:16:13 2025 -0400 +++ b/src/Replacer/TableReplacer.php Thu Oct 16 10:54:04 2025 -0400 @@ -112,7 +112,7 @@ $options = []; // Add default value checks if needed - if (is_null($column['default']) && ! $column['nullable']) { + if (/*is_null($column['default']) && */! $column['nullable']) { $options[] = "'required'"; } else {
