Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.0] Problem with inversed OneToOne relationships #655

Open
simondaigre opened this issue Jun 27, 2024 · 5 comments · May be fixed by #659
Open

[2.0] Problem with inversed OneToOne relationships #655

simondaigre opened this issue Jun 27, 2024 · 5 comments · May be fixed by #659
Labels
bug Something isn't working

Comments

@simondaigre
Copy link

simondaigre commented Jun 27, 2024

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 ?

@simondaigre simondaigre changed the title [2.0] Can't use Reusable Factory "States" (->with()) in defaults() method [2.0] Can't use Reusable Factory "States" (->with()) in defaults() method Jun 27, 2024
@nikophil
Copy link
Member

Hi @simondaigre

With the Rector rule, the upgrade is really smooth

nice to hear that! 😊

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

@nikophil
Copy link
Member

nikophil commented Jun 27, 2024

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

// 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
Copy link
Author

@nikophil
Copy link
Member

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

@nikophil nikophil changed the title [2.0] Can't use Reusable Factory "States" (->with()) in defaults() method [2.0] Problem with inversed OneToOne relationships Jun 28, 2024
@nikophil nikophil linked a pull request Jun 28, 2024 that will close this issue
@mmarton
Copy link

mmarton commented Sep 27, 2024

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

@nikophil nikophil added the bug Something isn't working label Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

Successfully merging a pull request may close this issue.

3 participants