fzaninotto / Faker

Faker is a PHP library that generates fake data for you
MIT License
26.78k stars 3.57k forks source link

Wrong field length when populating entities using an ORM #1480

Open robertkabat opened 6 years ago

robertkabat commented 6 years ago

Hello,

I got simple Doctrine entity with 3 fields on it:

    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     * @Groups({"default"})
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=15, nullable=true, options={"default":null})
     * @Groups({"default"})
     */
    private $status;

    /**
     * @ORM\Column(type="string", length=15, nullable=true, options={"default":null})
     * @Groups({"default"})
     */
    private $description;

Now I am trying to populate that entity with some random data(this is just example, actually I got bunch of entities but this is the first that's causing a problem I assume I would get that with some other entities as well):

    $manager = $this->managerRegistry->getManager();
    $generator = \Faker\Factory::create();
    $populator = new Populator($generator, $manager);
    $populator->addEntity(PostStatus::class, 1);
    dump($populator->execute()); exit;

The problem occurs while preparing $description. For some reason length of that field is not being captured.

I went to /vendor/fzaninotto/faker/src/Faker/ORM/Doctrine/EntityPopulator.php to guessColumnFormatters method and I've noticed that for some reason for this one field size in not captured - at least that's what I think I didn't study the whole thing.

As a result I get field that should have 50 character top with string longer then 50 characters. That of course fails while I'm trying to save that to database.

Is there anything wrong I am doing here? I really can't see any solution here. I will have to populate many entities like that and I wanted to automate that process and I would like to avoid specifying dummy data manually.

Did I get anything wrong here? If that's not a bug but just my mistake could someone please be so kind and point me in the right direction.

Regards, Rob

UPDATE

I got to the story/vendor/fzaninotto/faker/src/Faker/Guesser/Name.php and I can see here:

        case 'description': dump($generator->text); exit;
            return function () use ($generator) {
                return $generator->text;
            };

That you are not checking for the length. Shouldn't it take the length from entity's annotation and trim the string or generate proper length?

pimjansen commented 5 years ago

@fzaninotto what are the original intentions from the ORM implementation. Just to populate the data or also the take the schema into account like mentioned above.

fzaninotto commented 5 years ago

yes, we should take the schema into account as much as possible to generate proper data.

pimjansen commented 5 years ago

@fzaninotto im a bit struggling with the solution here though since the provider will generate a name base on field guess. However we do not have a max chars or something and only on text we can actually use it performance wise as well.

We could decide to strip the chars if they are too long though even though that wont be a very neath solution either.

fzaninotto commented 5 years ago

maybe the ColumnTypeGuesser should return an object rather than just a string?