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
643 stars 70 forks source link

[2.0] Problem with inversed `OneToOne` relationships #655

Open simondaigre opened 3 months ago

simondaigre commented 3 months ago

Hello there,

I'm upgrading a project from 1.38 to 2.0. With the Rector rule, the upgrade is really smooth, thanks @nikophil !

I just have a minor issue, in one Factory, in the defaults() method, I'm doing something like this :

protected function defaults(): array
{
    return [
        'customer' => CustomerFactory::new()->withUser(),
    ];
}

And in Customer factory :

public function withUser(): self
{
    return $this->with(static fn (): array => ['user' => UserFactory::new()]);
}

With Foundry 1.38, my Customer was containing an User, since 2.0 User is always null. Is it something deprecated or a bug ?

nikophil commented 3 months ago

Hi @simondaigre

With the Rector rule, the upgrade is really smooth

nice to hear that! :blush:

You problem definitively sounds like a bug. I'm trying to reproduce it.

nikophil commented 3 months ago

hmm I kinda made the same thing, but everything is hydrated as expected :thinking:

// Object1Factory
    protected function defaults(): array|callable
    {
        return [
            'object2' => Object2Factory::new()->withSomeObject(),
        ];
    }

// Object2Factory
    public function withSomeObject(): static
    {
        return $this->with(static fn (): array => ['object3' => Object3Factory::new(['prop1' => 'toto'])]);
    }

any chance you create a public reproducer please?

simondaigre commented 3 months ago

https://github.com/simondaigre/foundry-repro

nikophil commented 3 months ago

ok, I confirm that there is a bug: CustomerFactory::new()->withUser()->create(); this creates two "customers"

mmarton commented 2 days ago

Any update on this one? I have the same issue.