You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'd say this issue does not have a simple solution and it just requires documentation change.
Assume we have following entities:
#[Entity]
class Foo
{
#[OneToMany(mappedBy: "foo", targetEntity: Bar::class, orphanRemoval: true)]
publicCollection$collection;
publicfunction__construct()
{
$this->collection = newArrayCollection();
}
}
#[Entity]
class Bar
{
#[JoinColumn]
#[ManyToOne(targetEntity: Foo::class, inversedBy: "collection")]
publicFoo$foo;
}
Current behavior
Following code will create an entity even though the collection should be empty:
$foo = newFoo();
$bar = newBar();
$bar->foo = $foo;
$entityManager->persist($bar);
$foo->collection->add($bar);
$foo->collection->clear();
// Bar will be created in the DB and hydrated on refresh$entityManager->flush();
The issue is caused by combination of orphanRemoval with manually calling $entityManager->persist($entity) which is hard to fix (as ArrayCollection does not have access to entity manager) yet undocumented so unexpected.
Following code will work as expected:
// Working - Bar will be created & deleted$foo = newFoo();
$bar = newBar();
$bar->foo = $foo;
$entityManager->persist($bar);
$foo->collection->add($bar);
// Now the collection becomes PersistentCollection which does propagate changes to UnitOfWork$entityManager->flush();
$foo->collection->clear();
$entityManager->flush();
Expected behavior
The only possible solution that comes to my mind is to document this behavior in orphanRemoval part and recommend using it alongside cascade=["persist"]
The text was updated successfully, but these errors were encountered:
Bug Report
Summary
I'd say this issue does not have a simple solution and it just requires documentation change.
Assume we have following entities:
Current behavior
Following code will create an entity even though the collection should be empty:
The issue is caused by combination of
orphanRemoval
with manually calling$entityManager->persist($entity)
which is hard to fix (as ArrayCollection does not have access to entity manager) yet undocumented so unexpected.Following code will work as expected:
Expected behavior
The only possible solution that comes to my mind is to document this behavior in
orphanRemoval
part and recommend using it alongsidecascade=["persist"]
The text was updated successfully, but these errors were encountered: