doctrine / data-fixtures

Doctrine2 ORM Data Fixtures Extensions
http://www.doctrine-project.org
MIT License
2.77k stars 224 forks source link

Add a getFixture method #238

Closed mgirouard closed 8 years ago

mgirouard commented 8 years ago

I'm not sure if this is a good idea, but I've run into the case a few times where you need to get a fixture instance you didn't directly instantiate. Most often this happens when one fixture is automatically added by the loader as a dependency on another fixture.

mgirouard commented 8 years ago

Any feedback on this? Happy to close if it's not useful!

Ocramius commented 8 years ago

@mgirouard can you show where/how this would be used?

mgirouard commented 8 years ago

We ran into a a problem where we didn't have full control over the addFixture() calls on a Loader. More specifically, we didn't have control over the order in which fixtures were added. Most of our fixtures implement DependentFixtureInterface so we couldn't rely on constructor.

Instead, we have to do something similar to:

foreach ($loader->getFixtures() as $fixture) {
    if ($fixture instanceof Fixtures\Foo) {
        $setting = $container->get('settings')['fooSetting'];
        $fixture->configureSetting($setting);
        break;
    }
}

Ideally we can just ask for the fixture by FQCN:

$fixture = $loader->getFixture(Fixtures\Foo::class);

We've tried to restructure a bit, but this seems to be the simplest possible solution.

mikeSimonson commented 8 years ago

@Ocramius Seems good to go to me ?

Ocramius commented 8 years ago

Indeed, good to go.

mgirouard commented 8 years ago

Thanks all. I'm looking forward to using this right away!