Open
Description
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 :)
Activity