- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一些具有抽象类的实体:EntityDated
这意味着该实体包含 4 个公共(public)字段:created
、updated
、创建者
和更新者
。我想在创建实体时更新 4 数据,并在更新实体时更新“updated
”和“updated_by
”。
我做了一个调用我的监听器的服务:
public function preFlush(PreFlushEventArgs $eventArgs)
{
$token = $this->container->get('security.context')->getToken();
$em = $eventArgs->getEntityManager();
$uow = $em->getUnitOfWork();
// Inserts
foreach ($uow->getScheduledEntityInsertions() as $entity) {
if (is_subclass_of($entity, 'Kiwi\Bundle\TrainingBundle\Entity\EntityDated')) {
$entity->setCreated(new \Datetime());
$entity->setCreatedBy($token->getUser()->getUsername());
$entity->setUpdated(new \Datetime());
$entity->setUpdatedBy($token->getUser()->getUsername());
}
}
// Updates
foreach ($uow->getScheduledEntityUpdates() as $entity) {
if (is_subclass_of($entity, 'Kiwi\Bundle\TrainingBundle\Entity\EntityDated')) {
$entity->setUpdated(new \Datetime());
$entity->setUpdatedBy($token->getUser()->getUsername());
}
}
}
这仅适用于 INSERTS,不适用于 UPDATES(更改已保存,但更新和更新保持不变)。如果我调试 $uow->getScheduledEntityUpdates()
,我发现它是空的。为什么不在此处管理更新的实体?
最佳答案
为了同时执行插入和更新,我必须更改两点:
onFlush()
,而不是preFlush()
新代码:
public function onFlush(OnFlushEventArgs $eventArgs)
{
$token = $this->container->get('security.context')->getToken();
$em = $eventArgs->getEntityManager();
$uow = $em->getUnitOfWork();
// Inserts
foreach ($uow->getScheduledEntityInsertions() as $entity) {
if (is_subclass_of($entity, 'Kiwi\Bundle\TrainingBundle\Entity\EntityDated')) {
$entity->setCreated(new \Datetime());
$entity->setCreatedBy($token->getUser()->getUsername());
$entity->setUpdated(new \Datetime());
$entity->setUpdatedBy($token->getUser()->getUsername());
$meta = $em->getClassMetadata(get_class($entity));
$uow->recomputeSingleEntityChangeSet($meta, $entity);
}
}
// Updates
foreach ($uow->getScheduledEntityUpdates() as $entity) {
if (is_subclass_of($entity, 'Kiwi\Bundle\TrainingBundle\Entity\EntityDated')) {
$entity->setUpdated(new \Datetime());
$entity->setUpdatedBy($token->getUser()->getUsername());
$meta = $em->getClassMetadata(get_class($entity));
$uow->recomputeSingleEntityChangeSet($meta, $entity);
}
}
}
关于doctrine-orm - 学说 2 : preFlush getScheduledEntityInsertions() works but not getScheduledEntityUpdates(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26277625/
我刚刚注意到 Doctrine 事件系统的奇怪行为。在文档中,我阅读了有关 preFlush 事件的信息: preFlush is called at EntityManager#flush() be
我正在尝试将用户实体添加到 preFlush 事件上的实体。基本上每个实体都有一个 createdBy,它是一个 User 实体。 每当我尝试此操作时,都会收到以下错误。 Notice: Undefi
我为给定实体 Foo 创建了一个实体监听器,它监听 preFlush 事件。每当更新或创建 Foo 实体时,我都想创建一个新的 Bar 实体。我的问题是 computeChangeSets() 再次触
我有一个 onFlush() 事件,工作正常,但我需要做的是将其转换为 preFlush() 或 preUpdate() 都可以接受。我做了 preFlush() 但由于某种原因它没有做任何事情。甚至
我有一些具有抽象类的实体:EntityDated 这意味着该实体包含 4 个公共(public)字段:created、updated、创建者 和更新者。我想在创建实体时更新 4 数据,并在更新实体时更
我是一名优秀的程序员,十分优秀!