教义可责怪的扩展“改变"不起作用 [英] Doctrine blameable extension 'on change' doesn't work
本文介绍了教义可责怪的扩展“改变"不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用带有stof教义扩展名的symfony 2.6.3.
I'm on symfony 2.6.3 with stof Doctrine extension.
TimeStampable和SoftDeletable可以很好地工作.
TimeStampable and SoftDeletable work well.
可责怪的创建时"和更新时"也能很好地发挥作用:
Also Blameable "on create" and "on update" are working well too:
/**
* @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;
但是不断变化"似乎不起作用.
But "on change" seems not to be working.
/**
* @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
.
I've got SoftDeletable configured on "deletedAt" field. SoftDeletable works fine, but deletedBy
is never filled.
我如何设法使其正常工作?我只想设置删除该实体的用户ID.
How can I manage to make it work? I just want to set user id who deleted the entity.
推荐答案
这是我的解决方法:
Here my solution :
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)));
}
}
}
}
这篇关于教义可责怪的扩展“改变"不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文