“在变革中”的学说责备延期不起作用

时间:2015-02-02 10:08:58

标签: php symfony onchange stofdoctrineextensions

我在symfony 2.6.3上使用stof Doctrine扩展名。

TimeStampable和SoftDeletable运作良好。

同样可以责备" on create"和"更新"工作得很好:

/**
 * @var User $createdBy
 *
 * @Gedmo\Blameable(on="create")
 * @ORM\ManyToOne(targetEntity="my\TestBundle\Entity\User")
 * @ORM\JoinColumn(name="createdBy", referencedColumnName="id")
 */
protected $createdBy;

/**
 * @var User $updatedBy
 *
 * @Gedmo\Blameable(on="update")
 * @ORM\ManyToOne(targetEntity="my\TestBundle\Entity\User")
 * @ORM\JoinColumn(name="updatedBy", referencedColumnName="id")
 */
protected $updatedBy;

但是"关于改变"似乎不起作用。

/**
 * @var User $deletedBy
 *
 * @Gedmo\Blameable(on="change", field="deletedAt")
 * @ORM\ManyToOne(targetEntity="my\UserBundle\Entity\User")
 * @ORM\JoinColumn(name="deletedBy", referencedColumnName="id")
 */
protected $deletedBy;

我已经在" deletedAt"上配置了SoftDeletable。领域。 SoftDeletable工作正常,但deletedBy永远不会被填充。

如何让它成功?我只想设置删除实体的用户ID。

4 个答案:

答案 0 :(得分:1)

问题是你想在它上面调用remove方法时更新实体(设置用户)。

目前,可能没有一个完美的解决方案来注册使用Softdeleteable + Blameable扩展软删除对象的用户。

一些想法可能是覆盖SoftDeleteableListener(https://github.com/Atlantic18/DoctrineExtensions/blob/master/lib/Gedmo/SoftDeleteable/SoftDeleteableListener.php),但我遇到了问题。

我目前使用的解决方案是使用Entity Listener Resolver。

MyEntity.php

/**
* @ORM\EntityListeners({„Acme\MyBundle\Entity\Listener\MyEntityListener" })
*/

class MyEntity {

/**
* @ORM\ManyToOne(targetEntity="Acme\UserBundle\Entity\User")
* @ORM\JoinColumn(name="deleted_by", referencedColumnName="id")
*/
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
{
/**
 * @var TokenStorageInterface
 */
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();
    }
}

}

这里的一个不完美就是调用flush方法。

注册服务:

services:
    myentity.listener.resolver:
        class: Acme\MyBundle\Entity\Listener\MyEntityListener
        arguments:
            - @security.token_storage
        tags:
            - { name: doctrine.orm.entity_listener, event: preRemove }

更新composer.json中的doctrine / doctrine-bundle:

"doctrine/doctrine-bundle": "1.3.x-dev"

如果您有任何其他解决方案,特别是有关SoftDeleteableListener的解决方案,请在此处发布。

答案 1 :(得分:1)

  

这是我的解决方案:

     
mybundle.soft_delete:
 class: Listener\SoftDeleteListener
 arguments:
    - @security.token_storage
 tags:
    - { name: doctrine_mongodb.odm.event_listener, event: preSoftDelete }
  
class SoftDeleteListener
{
/**
 * @var TokenStorageInterface
 */
private $tokenStorage;

public function __construct(TokenStorageInterface $tokenStorage)
{
    $this->tokenStorage = $tokenStorage;
}

/**
 * Method called before "soft delete" system happened.
 *
 * @param LifecycleEventArgs $lifeCycleEvent Event details.
 */
public function preSoftDelete(LifecycleEventArgs $lifeCycleEvent)
{
    $document = $lifeCycleEvent->getDocument();
    if ($document instanceof SoftDeletedByInterface) {
        $token = $this->tokenStorage->getToken();

        if (is_object($token)) {
            $oldValue = $document->getDeletedBy();
            $user = $token->getUser();

            $document->setDeletedBy($user);
            $uow = $lifeCycleEvent->getObjectManager()->getUnitOfWork();
            $uow->propertyChanged($document, 'deletedBy', $oldValue, $user);
            $uow->scheduleExtraUpdate($document, array('deletedBy' => array($oldValue, $user)));
        }
    }
}

}

答案 2 :(得分:1)

这是我的解决方案,我使用preSoftDelete事件:

app.event.entity_delete:
    class: AppBundle\EventListener\EntityDeleteListener
    arguments:
        - @security.token_storage
    tags:
        - { name: doctrine.event_listener, event: preSoftDelete, connection: default }

和服务:

<?php

namespace AppBundle\EventListener;

use Doctrine\ORM\Event\LifecycleEventArgs;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

class EntityDeleteListener
{
    /**
     * @var TokenStorageInterface
     */
    private $tokenStorage;

    public function __construct(TokenStorageInterface $tokenStorage)
    {
        $this->tokenStorage = $tokenStorage;
    }

    public function preSoftDelete(LifecycleEventArgs $args)
    {
        $token  = $this->tokenStorage->getToken();
        $object = $args->getEntity();
        $om     = $args->getEntityManager();
        $uow    = $om->getUnitOfWork();

        if (!method_exists($object, 'setDeletedBy')) {
            return;
        }

        if (null == $token) {
            throw new AccessDeniedException('Only authorized users can delete entities');
        }

        $meta = $om->getClassMetadata(get_class($object));
        $reflProp = $meta->getReflectionProperty('deletedBy');
        $oldValue = $reflProp->getValue($object);
        $reflProp->setValue($object, $token->getUser()->getUsername());

        $om->persist($object);
        $uow->propertyChanged($object, 'deletedBy', $oldValue, $token->getUser()->getUsername());
        $uow->scheduleExtraUpdate($object, array(
            'deletedBy' => array($oldValue, $token->getUser()->getUsername()),
        ));
    }
}

这不是一致性,因为我检查setDeletedBy方法是否存在并设置了deletedBy属性,但它对我有用,你可以根据需要升级这段代码

答案 3 :(得分:0)

这是我发现的另一种解决方案:

注册服务:

softdeleteable.listener:
  class: AppBundle\EventListener\SoftDeleteableListener
  arguments:
    - '@security.token_storage'
  tags:
    - { name: doctrine.event_listener, event: preFlush, method: preFlush }

SoftDeleteableListener:

/**
 * @var TokenStorageInterface|null
 */
private $tokenStorage;

/**
 * DoctrineListener constructor.
 *
 * @param TokenStorageInterface|null $tokenStorage
 */
public function __construct(TokenStorageInterface $tokenStorage)
{
    $this->tokenStorage = $tokenStorage;
}

/**
 * @param PreFlushEventArgs $event
 */
public function preFlush(PreFlushEventArgs $event)
{
    $user = $this->getUser();
    $em   = $event->getEntityManager();

    foreach ($em->getUnitOfWork()->getScheduledEntityDeletions() as $object) {
        /** @var SoftDeleteableEntity|BlameableEntity $object */
        if (method_exists($object, 'getDeletedBy') && $user instanceof User) {
            $object->setDeletedBy($user);
            $em->merge($object);

            // Persist and Flush allready managed by other doctrine extensions.
        }
    }
}

/**
 * @return User|void
 */
public function getUser()
{
    if (!$this->tokenStorage || !$this->tokenStorage instanceof TokenStorageInterface) {
        throw new \LogicException('The SecurityBundle is not registered in your application.');
    }

    $token = $this->tokenStorage->getToken();
    if (!$token) {
        /** @noinspection PhpInconsistentReturnPointsInspection */
        return;
    }

    $user = $token->getUser();
    if (!$user instanceof User) {
        /** @noinspection PhpInconsistentReturnPointsInspection */
        return;
    }

    return $user;
}