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

Bug: doctrine identity problem when using $this in a method of proxified objects #710

Open
KDederichs opened this issue Oct 28, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@KDederichs
Copy link
Contributor

KDederichs commented Oct 28, 2024

Hey,

I just updated to 2.2 from 1.38 and I found an issue where foundry doesn't correctly handle objects that are created by proxy objects.

Given that you have a class Foo:

class Foo
{

    #[OneToMany(mappedBy: 'foo', targetEntity: FooBar::class, cascade: ['persist', 'remove'], fetch: 'EAGER', orphanRemoval: true)]
    private Collection $fooBarCollection;

    public function addBarToFoo(Bar $bar): self 
    {
        $fooBar = new FooBar();
        $fooBar->setBar($bar)->setFoo($this);

        return $this.
    }

    public function createABarAndAddToFoo(): self 
    {
        $bar = new Bar():
        return $this->addBarToFoo($bar);
    }
}

class Bar
{
    //something
}

class FooBar {
    
    #[ManyToOne(targetEntity: Foo::class, inversedBy: 'fooBarCollection')]
    private Foo $foo;

    #[ManyToOne(targetEntity: Bar::class, cascade: ['persist'], fetch: 'EAGER')]  
    private Bar $bar;

    public function setFoo(Foo $foo): self
    {
        $this->foo = $foo;
    
        return $this;
    }

    public function setBar(Bar $bar): self
    {
        $this->bar = $bar;

        return $this;
    }
}

When you create a new Foo using FooFactory::createOne() and then call createABarAndAddToFoo() and _save() on the created Foo object this will result in an error like this:

Doctrine\ORM\ORMInvalidArgumentException: A new entity was found through the relationship 'App\Entity\FooBar#foo' that was not configured to cascade persist operations for entity: XXX. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}).

This error obviously should happen since the Foo object has been created by the factory earlier and thus exists/is persisted.
This was not an issue in the 1.x version of Foundry.

You can 'fix' it by prefixing the createABarAndAddToFoo call with a _real() but I'd rather have it work correctly :)

@nikophil
Copy link
Member

Hello,

please, could you share your factories?

@KDederichs
Copy link
Contributor Author

https://github.com/KDederichs/sf-reproducer/tree/foundy/proxy_save here's a reproducer for that, just default factories as they come out of the box

@nikophil
Copy link
Member

thank you for the reproducer, I'll soon check this!

@nikophil
Copy link
Member

nikophil commented Nov 22, 2024

hye @KDederichs sorry for the late reply.

this problem gaves us headaches 🤯

for now, this cannot be fixed in an other way than using ->_real() 🤷

@KDederichs
Copy link
Contributor Author

Haha yeah it's a strange one for sure, I'll keep using _real() when I run into it for sure till you eventually figure it out :)

@nikophil nikophil changed the title [2.x] Proxy objects creating objects results in non persistent entities Bug: doctrine identity problem when using $this in a method of proxified objects Dec 8, 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

No branches or pull requests

2 participants