I want to remove an object that is an attribute of another object, but when I do this I get an exception saying “Entity was not found.”
An example of what I mean:
// Foo has a private property, bar, which is an instance of Bar
$foo = $this->get('my_bundle.repository.foo')->find($fooId);
$bar = $foo->getBar();
$entityManager = $this->get('doctrine.orm.entity_manager');
$entityManager->remove($bar);
$entityManager->flush();
I guess this is because the entity manager didn’t load an instance of Bar directly, as the following seems to work:
// Foo has a private property, bar, which is an instance of Bar
$foo = $this->get('my_bundle.repository.foo')->find($fooId);
$bar = $this->get('my_bundle.repository.foo')->find($bar->getId());
$entityManager = $this->get('doctrine.orm.entity_manager');
$entityManager->remove($bar);
$entityManager->flush();
Is there a way to make this work without reloading $bar?
The problem is that Foo still has Bar as a property when you flush the entity manager. Do
before you do the flush