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

Disable softdeleteable filter doesn't disabled Listeners #1175

Open
Garfield-fr opened this issue Oct 14, 2014 · 8 comments

Comments

@Garfield-fr
Copy link

commented Oct 14, 2014

I would like to hard-delete a record by disabling the filter but it doesn't work because listeners are already active on my entity.

I find a solution but it's not perfect:

$manager->getFilters()->disable('softdeleteable');
foreach ($manager->getEventManager()->getListeners() as $eventName => $listeners) {
    foreach ($listeners as $listener) {
        if ($listener instanceof \Gedmo\SoftDeleteable\SoftDeleteableListener) {
            $manager->getEventManager()->removeEventListener($eventName, $listener);
        }
    }
}

Do you have another solution ?

Thank's

@Garfield-fr Garfield-fr changed the title Disable softdeleteable filter doesn't disabled Listener Disable softdeleteable filter doesn't disabled Listeners Oct 14, 2014
@gitomato

This comment has been minimized.

Copy link

commented Dec 10, 2014

Hello, it may be a typo.
Can you try with :

$manager->getFilters()->disable('soft-deleteable');

https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/softdeleteable.md#setup-and-autoloading

@Garfield-fr

This comment has been minimized.

Copy link
Author

commented Dec 23, 2014

doctrine:
        orm:
            entity_managers:
                default:
                    filters:
                        softdeleteable:
                            class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
                            enabled: true

and in my function

$em = $this->getContainer()->get('doctrine')->getManager();
$em->getFilters()->disable('softdeleteable');
...
$em->remove($user);
$em->flush();

The record is not deleted from my db, it's marked deleted with date and time. Disabled filter don't deactivated softdeletable listener.

Any solution ?

Thank's

@appeltaert

This comment has been minimized.

Copy link

commented Oct 19, 2015

same, doesnt seem fixed 1 year later? theres no way to disable them now. @Garfield-fr did you find a solution?

@max-kovpak

This comment has been minimized.

Copy link

commented Oct 20, 2015

You can create your own event listener something like this:

<?php namespace AppBundle\EventListener;

use \Gedmo\SoftDeleteable\SoftDeleteableListener as BaseSoftDeleteableListener;
use Doctrine\Common\EventArgs;

class SoftDeleteableListener extends BaseSoftDeleteableListener
{

    /**
     * @inheritdoc
     */
    public function onFlush(EventArgs $args)
    {
        $ea = $this->getEventAdapter($args);
        $om = $ea->getObjectManager();
        //return from event listener if you disabled filter: $em->getFilters()->disable('softdeleteable');
        if (!$om->getFilters()->isEnabled('softdeleteable')) {
            return;
        }

        parent::onFlush($args);
    }

}

in your config:

    gedmo.listener.softdeleteable:
        class: AppBundle\EventListener\SoftDeleteableListener
        tags:
            - { name: doctrine.event_subscriber, connection: default }
        calls:
            - [ setAnnotationReader, [ @annotation_reader ] ]
@keksa

This comment has been minimized.

Copy link

commented Jun 6, 2016

For anyone still wondering about this. It is possible to hard delete softdeleteable entity with provided listener by setting the deletedAt field to DateTime object.

So for example this would hard delete user entity:

$user->setDeletedAt(new \DateTime);
$em->remove($user);
$em->flush();

This code in Gedmo listener provides that:

$reflProp = $meta->getReflectionProperty($config['fieldName']);
$oldValue = $reflProp->getValue($object);
if ($oldValue instanceof \Datetime) {
    continue; // want to hard delete
}
@cadavre

This comment has been minimized.

Copy link

commented Nov 3, 2017

Wait a second...

$reflProp = $meta->getReflectionProperty($config['fieldName']);
$oldValue = $reflProp->getValue($object);
if ($oldValue instanceof \Datetime) {
    continue; // want to hard delete
}

Doesn't it mean that if you call $em->remove($entity) for a second time (on already soft-deleted entity) it will remove it from DB?

@Deveosys

This comment has been minimized.

Copy link

commented Jan 29, 2018

Exactly, it's up to you not to make the deletion actions available on soft deleted objects

@l3pp4rd

This comment has been minimized.

Copy link
Member

commented Jan 29, 2018

there is an option now to prevent hard deletion on second call and any other, see the doc for softdelete

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
9 participants
You can’t perform that action at this time.