The problem is that you want to update the object (install the user) when you call the remove method on it.
Currently, there may not be an ideal solution for registering a user who deleted an object using Softdeleteable + Blameable extensions.
Some idea might be to overwrite SoftDeleteableListener ( https://github.com/Atlantic18/DoctrineExtensions/blob/master/lib/Gedmo/SoftDeleteable/SoftDeleteableListener.php ), but I had a problem with this.
My current working solution is to use the Entity receiver-receiver.
MyEntity.php
class MyEntity { private $deletedBy; public function getDeletedBy() { return $this->deletedBy; } public function setDeletedBy($deletedBy) { $this->deletedBy = $deletedBy; }
MyEntityListener.php
use Doctrine\ORM\Event\LifecycleEventArgs; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Acme\MyBundle\Entity\MyEntity; class MyEntityListener { private $token_storage; public function __construct(TokenStorageInterface $token_storage) { $this->token_storage = $token_storage; } public function preRemove(MyEntity $myentity, LifecycleEventArgs $event) { $token = $this->token_storage->getToken(); if (null !== $token) { $entityManager = $event->getObjectManager(); $myentity->setDeletedBy($token->getUser()); $entityManager->persist($myentity); $entityManager->flush(); } } }
Imperfection here calls the flush method.
Registration Service:
services: myentity.listener.resolver: class: Acme\MyBundle\Entity\Listener\MyEntityListener arguments: - @security.token_storage tags: - { name: doctrine.orm.entity_listener, event: preRemove }
Update doctrine / doctrine in composer.json:
"doctrine/doctrine-bundle": "1.3.x-dev"
If you have other solutions, especially when it comes to SoftDeleteableListener, post it here.
source share