shuba.ivan
shuba.ivan

Reputation: 4051

Doctrine event listener and softdeleted filter

I have entity answers and I use softdeleted filter for them, and when I remove entity in some action everything fine, I have deletedAt datetime, but when I try remove this entity in OnFlushEvent my entity is gone from DB, why ?

 public function onFlush(OnFlushEventArgs $args)
{
    $em = $args->getEntityManager();
    $uow = $em->getUnitOfWork();
    $em->getFilters()->enable('softdeleteable');
    foreach ($uow->getScheduledEntityUpdates() as $entity) {
        if ($entity instanceof Questions) {
            $existAnswers = $this->container->get('app.repository.question_answers')
                ->findOneBy(['questions' => $entity]);
                        $em->remove($existAnswers);                                                          
        }
    }
}

entity

 * @Gedmo\SoftDeleteable(fieldName="deletedAt")
 */
class QuestionAnswers

config service

    app.doctrine_listener:
    class: AppBundle\Listener\DoctrineListener
    calls:
        - [setContainer, ['@service_container']]
    tags:
        - { name: doctrine.event_subscriber, connection: default }

I checked, this filter enabled, I try force enable but no helped

Upvotes: 3

Views: 696

Answers (1)

DrKey
DrKey

Reputation: 3495

I think the issue might be related to subscribers priority.

In fact seems that also SoftDeleteableListener implements a subscriber which collect entities to softdelete using onFlush() event as we can see here. Therefore if your event is fired after the softdeletable one, your entities will be normally deleted from Doctrine.

To avoid this I would set priotity on your subscriber in order to fire your events before SoftDeleteableListener ones

app.doctrine_listener:
    class: AppBundle\Listener\DoctrineListener
    calls:
        - [setContainer, ['@service_container']]
    tags:
        - { name: doctrine.event_subscriber, connection: default, priority: -256 }

Upvotes: 1

Related Questions