# HG changeset patch # User Luka Sitas # Date 1760626444 14400 # Node ID 2cf26b593f4a4ec552914d26018e5abed80b9a57 # Parent b5c6ebd3354725c77892b24390568f95bb1cbed5 better support for different column types diff -r b5c6ebd33547 -r 2cf26b593f4a src/Generator/Factory/FactoryGenerator.php --- 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()"; + } } /** diff -r b5c6ebd33547 -r 2cf26b593f4a src/Generator/Model/stubs/model.stub --- 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; /** diff -r b5c6ebd33547 -r 2cf26b593f4a src/Generator/Requests/RequestGenerator.php --- 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')]); } } diff -r b5c6ebd33547 -r 2cf26b593f4a src/Generator/Test/stubs/test.stub --- 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 @@