0

We're dealing with a situation where a user is presented with a form to add/remove some simple OneToMany entities of a Parent entity.

After form submission we've replaced the array collection of the parent with a new one based on the form data.

Using cascade={"persist", "remove"} means that any new OneToMany entities are successfully persisted and added to the database when we persist and flush the Parent.

What we would like is for entities which are no longer in the OneToMany ArrayCollection to be automatically removed from the database during the Parent's persist and flush, thereby allowing us to delete the ugly code we wrote to remove them one by one.

Is this possible?

WalterEgo
  • 515
  • 6
  • 20
  • Try to add `orphanRemoval=true` to your association – chapay Mar 07 '15 at 13:19
  • Looking at http://stackoverflow.com/questions/25515007/doctrine-cascade-remove-vs-orphanremoval-true, I think you might be right @chapay, I'll have a play now – WalterEgo Mar 09 '15 at 10:15
  • Ah, no, this concerns automatically deleting the child OneToMany entities if the Parent entity is deleted. That's not actually what where doing here, so the ugly code lives on for the time being ;-( – WalterEgo Mar 09 '15 at 10:26
  • In this case could you provide some code how do you remove your entities from the `ArrayCollection`? – chapay Mar 12 '15 at 21:21
  • I tried simple code with removing entities from `ArrayCollection` and then persisting Parent entity and it worked well. – chapay Mar 13 '15 at 21:52
  • We tried creating an ArrayCollection of the child entities, setting the ArrayCollection of the parent Entity with this and persisting. It worked fine with new child entities, but didn't seem to remove the ones that were no longer there. Our ugly code is only 15 lines, not a disaster, but it would be nice to make it go away. – WalterEgo Mar 16 '15 at 09:44

0 Answers0