-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Disable softdeleteable filter doesn't disabled Listeners #1175
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
Comments
Hello, it may be a typo.
|
and in my function
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 |
same, doesnt seem fixed 1 year later? theres no way to disable them now. @Garfield-fr did you find a solution? |
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 ] ] |
For anyone still wondering about this. It is possible to hard delete softdeleteable entity with provided listener by setting the 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
} |
Wait a second...
Doesn't it mean that if you call |
Exactly, it's up to you not to make the deletion actions available on soft deleted objects |
there is an option now to prevent hard deletion on second call and any other, see the doc for softdelete |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Update: in SoftDeleteableListener.php if (isset($config['hardDelete']) && $config['hardDelete'] && $oldValue instanceof \DateTimeInterface && $oldValue <= $date) {
continue; // want to hard delete
} So, configuration in entity file can be /**
* @ORM\Entity(repositoryClass=MyEntityRepository::class)
* @ORM\Table(name="my_table_name")
* @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false, hardDelete=true) <==== SET hardDelete to true
*/
class MyEntity { ... } Then, there is some new behaviors.
$entity = new Entity();
$this->em->persist($entity);
$this->em->flush();
// create entity in database
$this->em->remove($entity);
$this->em->flush();
// Now entity has deletedAt field filled -> is soft-deleted
$this->em->remove($entity);
$this->em->flush();
// Now entity is hard deleted, no anymore in database. How delete without removing twice ? just add new \DateTime() to deleted at field. $entity = new Entity();
$this->em->persist($entity);
$this->em->flush();
// Here I have a new entity saved in database
$entity->setDeletedAt(new \DateTime());
$this->em->remove($entity);
$this->em->flush();
// Now entity is hard deleted, no anymore in database. due to the DateTimeInterface condition on top off the topic. Hope this will help =) |
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:
Do you have another solution ?
Thank's
The text was updated successfully, but these errors were encountered: