3

My entity User is related to other entities through OneToOne relations, and I'm cascading "delete" for all of them.

I'm using SoftDeleteable behavior extension, so when I remove a User, the entity is not actually removed from the database: the deletedAt field is simply set to DateTime(now), and so are all the deletedAt fields of the related entites.

Now, when I want to restore a User, I do as suggested in the docs:

$em->getFilters()->disable('soft-deleteable');
$user->setDeletedAt(null);

My problem is all the related entities stay deleted when I do this. Any idea how I could cascade the setDeleted(null) to all of them automatically?

Roubi
  • 1,989
  • 1
  • 27
  • 36

1 Answers1

3

Never used this extension but looking at open issues on GH repository I can see quite a few of them related to similar problems when working with associations:

I'd try first to disable explicitly the filter for each related entity:

$filter = $em->getFilters()->enable('soft-deleteable');
$filter->disableForEntity('Entity\Article');
$filter->disableForEntity('Entity\SomeOtherEntity');

Otherwise I'd add a Listener (and bind it to one of the Doctrine events) to cascade the restore outside of soft-deleteable.

dlondero
  • 2,539
  • 1
  • 24
  • 33
  • Thanks for your answer. I'll try to add a listener as you suggested, even if it doesn't look very easy. – Roubi Jul 20 '16 at 11:13
  • Easier than you think! See docs http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html – dlondero Jul 20 '16 at 15:57
  • 1
    Yes, binding an event is easy, what I couldn't find out is how to get all "cascaded" entities. Something like $object->getCascadedRelations(). The info can certainly be found in the mapping but I don't know how to extract it. – Roubi Jul 21 '16 at 06:30