foundry
foundry copied to clipboard
[Make:factory] Use field limit length when specified
Hi,
Command make:factory
create Factory with default Faker method according to field type, like self::faker()->text()
for a string
field
What would you think about use limit
property of fields and in order to generate more accurate Factory ?
For example, this field
#[ORM\Column(type: 'string', length: 40)]
private string $label;
could be rendered this way in Factory
'label' => self::faker()->text(40)
I think i can work on a PR for this if you agree with this idea
Yes, this makes sense. What happens now? Text is truncated or do we get an error?
Yes, this makes sense. What happens now? Text is truncated or do we get an error?
At the moment, an exception is throw
Doctrine\DBAL\Exception\DriverException: An exception occurred while executing a query: SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'label' at row 1
Ok, would you like to work on a pr?
Ok, would you like to work on a pr?
Yes I would like to do it
Hi @kbond
After thinking about it a bit, I'm not sure about my idea to make this change and I prefer to ask for your advice before jumping in.
What would you think about changing a bit the const ORM_defaults in MakeFactory.php in order to have something like
'STRING' => "self::faker()->text($length)"
I think this will allow to set a value for the $length
, while keeping the current behavior if there is no maximum length defined in the entity
~I'm not positive but could the default length's differ by db vendor? What about:~
~'STRING' => $length ? "self::faker()->text({$length})" : "self::faker()->text()"~
Oh, I misunderstood what you were describing.
I also confirmed $property['length']
is always available for string
types.
So what about:
'STRING' => "self::faker()->text({length})"
Then, in defaultPropertiesFor()
, replace {length}
with the actual value.