Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to really delete a softDeletable entity? #294

Closed
pfleu opened this issue Feb 28, 2016 · 7 comments
Closed

How to really delete a softDeletable entity? #294

pfleu opened this issue Feb 28, 2016 · 7 comments

Comments

@pfleu
Copy link

pfleu commented Feb 28, 2016

Hi, I've been searching for a while but couldn't find a solution.
Is there a way (from a controller) to actually remove a softDeletable entity from the database?
Am I missing an existing feature?
Thanks.

@nifr
Copy link

nifr commented Feb 29, 2016

Quick 'n dirty:

$entityManager = $this->getDoctrine()->getManager('default');
$eventManager = $entityManager->getEventManager();

// remove the softdeletable subscriber
$subscriber = $this->get('knp.doctrine_behaviors.softdeletable_subscriber');
$eventManager->removeEventListener($subscriber->getSubscribedEvents(), $subscriber);

// remove entity while the subscriber is removed
$entityManager->remove($entity);
$entityManager->flush();

// add back the subscriber
$eventManager->addEventSubscriber($subscriber);

@pfleu
Copy link
Author

pfleu commented Mar 1, 2016

@nifr : Thank you so much for your quick answer! This is just what I needed!

@pfleu
Copy link
Author

pfleu commented Mar 1, 2016

@nifr I got the error "you have requested a non-existent service". After a quick search, I found the service in orm-services.yml and had to set "public: true" in order to get your solution to work. But this change will be lost after running composer, right? Can I make this change in a custom file somewhere else in my app so it's not lost after a composer update?

@nifr
Copy link

nifr commented Mar 2, 2016

2 possible solutions:

  1. define your controller itself as a service and inject the subscriber-service explicitly
  2. create a factory-service that would return the subscriber service and call that one in your controller

@migmolrod
Copy link

migmolrod commented May 13, 2016

Maybe you can just overwrite the subscriber in your own services.yml?
I don't know if it's a bad practice. And even haven't tried it myself to see if it works, but I'll probably need to test it very soon, since I'll need to completely disable that same filter/subscriber in my backend (so administrators can check out which entities are "soft-deleted", you know)

# app/config/services.yml
services:
    knp.doctrine_behaviors.softdeletable_subscriber:
        class:   "%knp.doctrine_behaviors.softdeletable_subscriber.class%"
        public:  true
        arguments:
            - "@knp.doctrine_behaviors.reflection.class_analyzer"
            - "%knp.doctrine_behaviors.reflection.is_recursive%"
            - "%knp.doctrine_behaviors.softdeletable_subscriber.softdeletable_trait%"
        tags:
            - { name: doctrine.event_subscriber }

@pfleu
Copy link
Author

pfleu commented May 13, 2016

Thanks for the tip! I'll give it a try when I can.

@micronax
Copy link

The easiest trick to avoid any doctrine event listeners and "behaviours" is to use DQL:

$this->getDoctrine()->getManager()
             ->createQuery('DELETE FROM App\Entity\User m WHERE m.id = :id')
             ->setParameter('id', $user->getId())
             ->execute();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants