foundry icon indicating copy to clipboard operation
foundry copied to clipboard

[Make:factory] Use field limit length when specified

Open MrYamous opened this issue 2 years ago • 7 comments

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

MrYamous avatar Jul 11 '22 08:07 MrYamous

Yes, this makes sense. What happens now? Text is truncated or do we get an error?

kbond avatar Jul 11 '22 16:07 kbond

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

MrYamous avatar Jul 11 '22 18:07 MrYamous

Ok, would you like to work on a pr?

kbond avatar Jul 12 '22 11:07 kbond

Ok, would you like to work on a pr?

Yes I would like to do it

MrYamous avatar Jul 12 '22 16:07 MrYamous

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

MrYamous avatar Jul 18 '22 10:07 MrYamous

~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()"~

kbond avatar Jul 18 '22 12:07 kbond

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.

kbond avatar Jul 18 '22 12:07 kbond