zenstruck / foundry

A model factory library for creating expressive, auto-completable, on-demand dev/test fixtures with Symfony and Doctrine.
https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html
MIT License
608 stars 63 forks source link

Using story pools without persisting #573

Closed Brewal closed 5 days ago

Brewal commented 3 months ago

I'm using foundry without doctrine and manually save my objects with this function in my factories :

protected function initialize(): self
    {
        return $this->withoutPersisting()
            ->afterInstantiate(
                fn (Establishment $establishment) => $this->repository->save($establishment),
            );
    }

I'm facing an issue while using stories and pools.

If I call $this->addToPool() inside a story, I get the following error :

RuntimeException : Should not get doctrine's object manager when persist is disabled.

What I understood is that I wouldn't be able to use pools because of the normalizeObject private method of Story :

        // ensure proxies are persisted
        if (!$object->isPersisted()) {
            $object->save();
        }

Is there a workaround for this use case ?

Thanks to this : https://github.com/zenstruck/foundry/pull/488 we can globally disable persist. Would it make sense to check for this configuration in the Story ?

nikophil commented 5 days ago

this is fixed!