sonata-project / SonataAdminBundle

The missing Symfony Admin Generator
https://docs.sonata-project.org/projects/SonataAdminBundle
MIT License
2.11k stars 1.26k forks source link

Delete Event isn't triggered in Batch delete #6550

Closed zf2timo closed 3 years ago

zf2timo commented 4 years ago

Environment

PHP FPM Image with separated NGINX

Sonata packages

$ composer show --latest 'sonata-project/*'
sonata-project/admin-bundle                  3.78.1 3.78.1 The missing Symfony Admin Generator
sonata-project/block-bundle                  4.4.0  4.4.0  Symfony SonataBlockBundle
sonata-project/cache                         2.0.1  2.0.1  Cache library
sonata-project/doctrine-extensions           1.10.1 1.10.1 Doctrine2 behavioral extensions
sonata-project/doctrine-mongodb-admin-bundle 3.5.0  3.5.0  Symfony Sonata / Integrate Doctrine MongoDB ODM into the SonataAdminBundle
sonata-project/exporter                      2.4.1  2.4.1  Lightweight Exporter library
sonata-project/form-extensions               1.6.0  1.6.0  Symfony form extensions
sonata-project/twig-extensions               1.4.1  1.4.1  Sonata twig extensions

Symfony packages

$ composer show --latest 'symfony/*'
symfony/asset                      v4.4.15 v5.1.8  Symfony Asset Component
symfony/browser-kit                v4.4.15 v5.1.8  Symfony BrowserKit Component
symfony/cache                      v4.4.15 v5.1.8  Symfony Cache component with PSR-6, PSR-16, and tags
symfony/cache-contracts            v2.2.0  v2.2.0  Generic abstractions related to caching
symfony/config                     v4.4.15 v5.1.8  Symfony Config Component
symfony/console                    v4.4.15 v5.1.8  Symfony Console Component
symfony/css-selector               v4.4.15 v5.1.8  Symfony CssSelector Component
symfony/debug                      v4.4.15 v4.4.16 Symfony Debug Component
symfony/debug-bundle               v4.4.15 v5.1.8  Symfony DebugBundle
symfony/dependency-injection       v4.4.15 v5.1.8  Symfony DependencyInjection Component
symfony/doctrine-bridge            v4.4.15 v5.1.8  Symfony Doctrine Bridge
symfony/dom-crawler                v4.4.15 v5.1.8  Symfony DomCrawler Component
symfony/dotenv                     v4.4.15 v5.1.8  Registers environment variables from a .env file
symfony/error-handler              v4.4.15 v5.1.8  Symfony ErrorHandler Component
symfony/event-dispatcher           v4.4.15 v5.1.8  Symfony EventDispatcher Component
symfony/event-dispatcher-contracts v1.1.9  v2.2.0  Generic abstractions related to dispatching event
symfony/expression-language        v4.4.15 v5.1.8  Symfony ExpressionLanguage Component
symfony/filesystem                 v4.4.15 v5.1.8  Symfony Filesystem Component
symfony/finder                     v4.4.15 v5.1.8  Symfony Finder Component
symfony/flex                       v1.9.10 v1.9.10 Composer plugin for Symfony
symfony/form                       v4.4.15 v5.1.8  Symfony Form Component
symfony/framework-bundle           v4.4.15 v5.1.8  Symfony FrameworkBundle
symfony/http-client                v4.4.15 v5.1.8  Symfony HttpClient component
symfony/http-client-contracts      v2.3.1  v2.3.1  Generic abstractions related to HTTP clients
symfony/http-foundation            v4.4.15 v5.1.8  Symfony HttpFoundation Component
symfony/http-kernel                v4.4.15 v5.1.8  Symfony HttpKernel Component
symfony/inflector                  v4.4.15 v5.1.8  Symfony Inflector Component
symfony/intl                       v4.4.15 v5.1.8  A PHP replacement layer for the C intl extension that includes additional data from the ICU library.
symfony/mailer                     v4.4.15 v5.1.8  Symfony Mailer Component
symfony/maker-bundle               v1.22.0 v1.23.0 Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code.
symfony/mime                       v4.4.15 v5.1.8  A library to manipulate MIME messages
symfony/monolog-bridge             v4.4.15 v5.1.8  Symfony Monolog Bridge
symfony/monolog-bundle             v3.6.0  v3.6.0  Symfony MonologBundle
symfony/options-resolver           v4.4.15 v5.1.8  Symfony OptionsResolver Component
symfony/phpunit-bridge             v5.1.7  v5.1.8  Symfony PHPUnit Bridge
symfony/polyfill-intl-grapheme     v1.20.0 v1.20.0 Symfony polyfill for intl's grapheme_* functions
symfony/polyfill-intl-icu          v1.20.0 v1.20.0 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-intl-idn          v1.20.0 v1.20.0 Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions
symfony/polyfill-intl-normalizer   v1.20.0 v1.20.0 Symfony polyfill for intl's Normalizer class and related functions
symfony/polyfill-mbstring          v1.20.0 v1.20.0 Symfony polyfill for the Mbstring extension
symfony/polyfill-php72             v1.20.0 v1.20.0 Symfony polyfill backporting some PHP 7.2+ features to lower PHP versions
symfony/polyfill-php73             v1.20.0 v1.20.0 Symfony polyfill backporting some PHP 7.3+ features to lower PHP versions
symfony/polyfill-php80             v1.20.0 v1.20.0 Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions
symfony/process                    v4.4.15 v5.1.8  Symfony Process Component
symfony/property-access            v4.4.15 v5.1.8  Symfony PropertyAccess Component
symfony/property-info              v4.4.15 v5.1.8  Symfony Property Info Component
symfony/routing                    v4.4.15 v5.1.8  Symfony Routing Component
symfony/security-acl               v3.1.0  v3.1.0  Symfony Security Component - ACL (Access Control List)
symfony/security-bundle            v4.4.15 v5.1.8  Symfony SecurityBundle
symfony/security-core              v4.4.15 v5.1.8  Symfony Security Component - Core Library
symfony/security-csrf              v4.4.15 v5.1.8  Symfony Security Component - CSRF Library
symfony/security-guard             v4.4.15 v5.1.8  Symfony Security Component - Guard
symfony/security-http              v4.4.15 v5.1.8  Symfony Security Component - HTTP Integration
symfony/serializer                 v4.4.15 v5.1.8  Symfony Serializer Component
symfony/service-contracts          v2.2.0  v2.2.0  Generic abstractions related to writing services
symfony/stopwatch                  v4.4.15 v5.1.8  Symfony Stopwatch Component
symfony/string                     v5.1.7  v5.1.8  Symfony String component
symfony/translation                v4.4.15 v5.1.8  Symfony Translation Component
symfony/translation-contracts      v2.3.0  v2.3.0  Generic abstractions related to translation
symfony/twig-bridge                v4.4.15 v5.1.8  Symfony Twig Bridge
symfony/twig-bundle                v4.4.15 v5.1.8  Symfony TwigBundle
symfony/validator                  v4.4.15 v5.1.8  Symfony Validator Component
symfony/var-dumper                 v4.4.15 v5.1.8  Symfony mechanism for exploring and dumping PHP variables
symfony/var-exporter               v4.4.15 v5.1.8  A blend of var_export() + serialize() to turn any serializable data structure to plain PHP code
symfony/web-link                   v4.4.15 v5.1.8  Symfony WebLink Component
symfony/web-profiler-bundle        v4.4.15 v5.1.8  Symfony WebProfilerBundle
symfony/yaml                       v4.4.15 v5.1.8  Symfony Yaml Component

PHP version

$ php -v
PHP 7.4.11 (cli) (built: Oct 13 2020 10:09:45) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Xdebug v2.9.8, Copyright (c) 2002-2020, by Derick Rethans

Subject

When a batch delete is executed, there is no event fired for the Document.

Steps to reproduce

Register an delete Event

    public function onImageDelete(PersistenceEvent $event): void
    {
        $object = $event->getObject();
        if (!$object instanceof Image) {
            return;
        }

        $this->imageManager->deleteImage($object->getFileName());
    }

    public static function getSubscribedEvents(): array
    {
        return [
            'sonata.admin.event.persistence.pre_remove' => 'onImageDelete',
        ];
    }

Expected results

The onImageDelete function should be executed - even in Batch mode.

Actual results

Only the Document is deleted, but without executing the registered Event handler.

I can create a pull request with a fix, if you want.

kirya-dev commented 4 years ago

Please ensure that registered extension AdminEventExtension: https://github.com/sonata-project/SonataAdminBundle/blob/be653e2d320717f958ba8653464e9732bf463aaa/src/Event/AdminEventExtension.php#L112-L118

Or use doctrine remove events

VincentLanglet commented 4 years ago

You're right,

The deleteAction is calling $this->admin->delete() which calls modelManager->delete and trigger the event. The batchDeleteAction is calling directly $this->modelManager->batchDelete().

What did you have in mind to fix this ?

zf2timo commented 4 years ago

@VincentLanglet I would use the query to load all objects. These can then be passed to the delete method of the AbstractAdmin. From here also the pre and post delete event is fired.

VincentLanglet commented 4 years ago

@VincentLanglet I would use the query to load all objects. These can then be passed to the delete method of the AbstractAdmin. From here also the pre and post delete event is fired.

Then we would stop to use the modelManager batchDelete method https://github.com/sonata-project/SonataAdminBundle/blob/3.x/src/Controller/CRUDController.php#L177

I assume the bacthDelete method was a delete method optimized so I'm not sure it's a good idea to use multiple times the delete method: https://github.com/sonata-project/SonataDoctrineORMAdminBundle/blob/3.x/src/Model/ModelManager.php#L221 instead of the batchDelete one: https://github.com/sonata-project/SonataDoctrineORMAdminBundle/blob/3.x/src/Model/ModelManager.php#L537

Best would be to find another solution. cc @sonata-project/contributors

phansys commented 4 years ago

I agree that removing objects in a batch operation should behave like any single remove operation (including the extension logic, etc). Maybe we could let ModelManager::batchDelete() to return a generator, including the models that are successfully removed within a batch cycle, in order to let the caller to iterate over these results and fire required events. I know this approach should require more elaboration, but I let it here for discussion.

kirya-dev commented 4 years ago

What about this?

 public function batchActionDelete(ProxyQueryInterface $query)
    {
        $this->admin->checkAccess('batchDelete');

        $modelManager = $this->admin->getModelManager();

        try {
            foreach($modelManager->getObjectsToBatchDeleting($query) as $object) {
                $this->admin->delete($object);
            }

            $this->addFlash(
                'sonata_flash_success',
                $this->trans('flash_batch_delete_success', [], 'SonataAdminBundle')
            );
        } catch (ModelManagerException $e) {
            $this->handleModelManagerException($e);
            $this->addFlash(
                'sonata_flash_error',
                $this->trans('flash_batch_delete_error', [], 'SonataAdminBundle')
            );
        }

        return $this->redirectToList();
    }
phansys commented 4 years ago

What about this?

What you're proposing doesn't seem to be a batch operation, since delete() is called individually for each object.

kirya-dev commented 4 years ago

is called individually for each object.

This is exactly what we need!

kirya-dev commented 4 years ago

@phansys says any time ago:

batch operation should behave like any single remove operation (including the extension logic, etc)

And now you say other solve.

phansys commented 4 years ago

This is exactly what we need!

AFAIK, we need to trigger the events associated to the remove operation for the removed objects, not commit the transaction for each single object.

kirya-dev commented 4 years ago

Run deleting transactions its task of ModelManager

phansys commented 4 years ago

Run deleting transactions its task of ModelManager

I agree, and I'm not going against that premise.

kirya-dev commented 4 years ago

ModelManager and now run deleting for each objects as single query (using remove & flush every 20 iteration)

No difference if we run flush every 20 iterations or every 1. Difference in call clear of ObjectManager - free a memory So, im think my code suggeston have a chance to live.

In my example getObjectsToBatchDeleting($query) has iterator & clear manager logic every 20 iterations.

Im would like take attention that if call postRemove when deletion transation not executed - wrong behavior

I, as an ordinary user, expect that deleting many objects is equivalent to deleting each one separately.

VincentLanglet commented 4 years ago

You can edit a message instead of posting 6 consecutive messages.

In my example getObjectsToBatchDeleting($query) has iterator & clear manager logic every 20 iterations.

If we decide to lose the benefit of a batch operation, we can just do

$objects = $selectedModelQuery->execute();
foreach ($objects as $object) {
    $this->admin->delete($object);
}

And we could deprecate the BatchDelete method from ModelManagerInterface.

phansys commented 4 years ago

In case we keep the benefit of the batch operation, this is an example about what I've mentioned before: https://3v4l.org/HjjWH.

kirya-dev commented 4 years ago

Benefit in run flushing and crearing every 20 iterations? Hmm. You know that is useless? Database lock query results in your operation memory in any way? Calling clear clears only php application memory of extra entities.

Same problem here with exporting: https://github.com/sonata-project/exporter/issues/347

Im see one really benefit in clearing.

In case we keep the benefit of the batch operation, this is an example about what I've mentioned before: https://3v4l.org/HjjWH.

Im would like see that postRemove was executed after entity was realy deleted from DB. In can check here on rows count in my needs (for checking that left less than 100 rows and stop delition)

VincentLanglet commented 4 years ago

Benefit in run flushing and crearing every 20 iterations? Hmm. You know that is useless? Database lock query results in your operation memory in any way? Calling clear clears only php application memory of extra entities.

I won't say it's useless when it's in the doctrine documentation: https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/batch-processing.html#bulk-deletes If you still think so, you should tell them directly.

PeterZhukov commented 3 years ago

Idea: may be batch operations should trigger other event, like "batchDelete" where you receive all ids, and then decice what to do with them.

github-actions[bot] commented 3 years ago

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.