Nothing
Nothing

Reputation: 187

Sonata Batch Operations & Hooks Doctrine

i've created an admin panel with Sonata (Symfony2 Bundle http://sonata-project.org/about) and i've added an event in the preRemove hook (Doctrine).

In few words when i delete a record, run a curl to an other service. Everythings works.

But Sonata have also the "Batch Operations" where i can select multiple lines and choose "Delete all selected".

But, if i choose "delete all selected" the curl not started. The preRemove hook (Doctrine) it's not called.

There is someone that can help me?!

Thanks

Upvotes: 4

Views: 1915

Answers (2)

Adrian C.
Adrian C.

Reputation: 762

There is another option, you can use the preBatchAction hook in your admin class.

public function preBatchAction($actionName, ProxyQueryInterface $query, array & $idx, $allElements)
{
    if ($actionName == 'delete') {
         // logic here
    }
}

See https://sonata-project.org/bundles/admin/master/doc/reference/batch_actions.html#optional-executing-a-pre-batch-hook

Upvotes: 2

TautrimasPajarskas
TautrimasPajarskas

Reputation: 2796

It seems, that batch delete action really avoids calling preRemove method (see Sonata\AdminBundle\Controller\CRUDController::batchActionDelete which calls Sonata\DoctrineORMAdminBundle\Model\ModelManager::batchDelete).

However, you have options:

  1. Override that batchActionDelete by using custom controller extending CRUDController. SonataMediaBundle does that.
  2. Register real doctrine event (not recommended) (Symfony cookbook tutorial)

Upvotes: 2

Related Questions