symfony - StofDoctrineExtensionsBundle SoftDeleteable
问题描述
在 SoftDeleteable Listener 中使用 propertyChanged() 方法的目的是什么?
没有这个动作,一切都完美无缺。根据 Doctrine 的说法,propertyChanged() 将实体中的属性更改通知此 UnitOfWork。通知和下一步是什么?
class SoftDeleteableListener extends MappedEventSubscriber
{
/**
* Pre soft-delete event
*
* @var string
*/
const PRE_SOFT_DELETE = "preSoftDelete";
/**
* Post soft-delete event
*
* @var string
*/
const POST_SOFT_DELETE = "postSoftDelete";
/**
* {@inheritdoc}
*/
public function getSubscribedEvents()
{
return array(
'loadClassMetadata',
'onFlush',
);
}
/**
* If it's a SoftDeleteable object, update the "deletedAt" field
* and skip the removal of the object
*
* @param EventArgs $args
*
* @return void
*/
public function onFlush(EventArgs $args)
{
$ea = $this->getEventAdapter($args);
$om = $ea->getObjectManager();
$uow = $om->getUnitOfWork();
$evm = $om->getEventManager();
//getScheduledDocumentDeletions
foreach ($ea->getScheduledObjectDeletions($uow) as $object) {
$meta = $om->getClassMetadata(get_class($object));
$config = $this->getConfiguration($om, $meta->name);
if (isset($config['softDeleteable']) && $config['softDeleteable']) {
$reflProp = $meta->getReflectionProperty($config['fieldName']);
$oldValue = $reflProp->getValue($object);
$date = new \DateTime();
// Remove `$oldValue instanceof \DateTime` check when PHP version is bumped to >=5.5
if (isset($config['hardDelete']) && $config['hardDelete'] && ($oldValue instanceof \DateTime || $oldValue instanceof \DateTimeInterface) && $oldValue <= $date) {
continue; // want to hard delete
}
$evm->dispatchEvent(
self::PRE_SOFT_DELETE,
$ea->createLifecycleEventArgsInstance($object, $om)
);
$reflProp->setValue($object, $date);
$om->persist($object);
$uow->propertyChanged($object, $config['fieldName'], $oldValue, $date);
if ($uow instanceof MongoDBUnitOfWork && !method_exists($uow, 'scheduleExtraUpdate')) {
$ea->recomputeSingleObjectChangeSet($uow, $meta, $object);
} else {
$uow->scheduleExtraUpdate($object, array(
$config['fieldName'] => array($oldValue, $date),
));
}
$evm->dispatchEvent(
self::POST_SOFT_DELETE,
$ea->createLifecycleEventArgsInstance($object, $om)
);
}
}
}
解决方案
推荐阅读
- google-cloud-platform - 使用 Traefik 入口(TCP 负载均衡器)为 GKE Autopilot 启用 DDoS 保护
- xml - 使用 XSLT 展平 XML:为父节点生成 id
- python-3.x - Python 中的 Selenium 数据驱动测试
- javascript - 跨度换行
- python - 如何创建一个模型来存储与另一个模型相关的多个图像
- jenkins - 我们如何使用 hudson.util.HudsonFailedToLoad: org.jvnet.hudson.reactor.ReactorException: java.lang.NoSuchMethodError 重启 Jenkins 服务?
- xcode - 项目“Project-Name”已损坏,由于解析错误而无法打开
- amazon-web-services - python - gzip 字符串并上传到 s3
- vue.js - 如何在 macbook pro 上安装 vue-visjs?
- c# - 在dotnet core中,是否可以创建一个不引用标准目标框架的库项目?