vierge-noire / cakephp-fixture-factories

CakePHP Fixture Factories
https://vierge-noire.github.io/
MIT License
83 stars 21 forks source link

Feature Request: Ability to set nested values using `make` method #207

Closed ishanvyas22 closed 1 year ago

ishanvyas22 commented 1 year ago

Problem

Currently it is not possible to set values that are nested.

class ExampleFactory extends BaseFactory
{
    ...

    protected function setDefaultTemplate(): void
    {
        $this->setDefaultData(function (Generator $faker) {
            return [
                'name' => $faker->name,
                'data' => [
                    'mobile' => 123456,
                    'email' => $faker->email,
                ],
                'created' => FrozenTime::now(),
                'modified' => FrozenTime::now(),
            ];
        });
    }

    ...
}

For above factory, this works:

ExampleFactory::make(['name' => 'John Doe'])->persist();

this doesn't:

ExampleFactory::make(['data' => ['email' => 'foo@example.org']])->persist();

Above will overwrite the whole data array values instead of just updating the data.email field.

Proposal

It would be a nice to allow developers to set nested values as well.

Option 1

Simply provide array:

ExampleFactory::make(['data' => ['email' => 'foo@example.org']])->persist();

Option 2

Using dot notation:

ExampleFactory::make(['data.email' => 'foo@example.org'])->persist();

Whichever options works best.

pabloelcolombiano commented 1 year ago

Thanks for the proposal @ishanvyas22 ! The dot notation is indeed a nice way to specify that a nested value should be inserted.

@pakacuda is this good for you?

pabloelcolombiano commented 1 year ago

Merged with v3.9