gpt4 book ai didi

symfony - 使用事件监听器创建 preUpdate 或 preFlush 事件

转载 作者:行者123 更新时间:2023-12-02 18:27:00 27 4
gpt4 key购买 nike

我有一个 onFlush() 事件,工作正常,但我需要做的是将其转换为 preFlush() 或 preUpdate() 都可以接受。我做了 preFlush() 但由于某种原因它没有做任何事情。甚至没有一个错误。我错过了什么?

测试:我在 preFlush() 中放置了 exit 来查看它是否被调用。结果是:1,因此 foreach() 永远不会运行!它是一个空数组。我还测试了 preUpdate() ,其中的所有行都已运行,但没有插入数据。

public function preFlush(PreFlushEventArgs $args)
{
$em = $args->getEntityManager();
$uow = $em->getUnitOfWork();
echo '1';
foreach ($uow->getScheduledEntityUpdates() as $entity) {
echo '2';
if ($entity instanceof User) {
echo '3';
}
}
exit;
}

我在阅读 documentation 后创建了它们.

service.yml

services:
entity.event_listener.user:
class: Site\FrontBundle\EventListener\Entity\UserListener
tags:
- { name: doctrine.event_listener, event: preUpdate }
- { name: doctrine.event_listener, event: onFlush }
- { name: doctrine.event_listener, event: preFlush }

使用 Flush() 示例:

class UserListener
{
public function onFlush(OnFlushEventArgs $args)
{
$em = $args->getEntityManager();
$uow = $em->getUnitOfWork();

foreach ($uow->getScheduledEntityUpdates() as $entity) {
if ($entity instanceof User) {
$userLog = new UserLog();
$userLog->setDescription($entity->getId() . ' being updated.');

$em->persist($userLog);

// Instead of $em->flush() cos we're already in flush process
$userLogMetadata = $em->getClassMetadata(get_class($userLog));
$uow->computeChangeSet($userLogMetadata, $userLog);
}
}
}
}

不工作 preFlush() 示例:

class UserListener
{
public function preFlush(PreFlushEventArgs $args)
{
$em = $args->getEntityManager();
$uow = $em->getUnitOfWork();

foreach ($uow->getScheduledEntityUpdates() as $entity) {
if ($entity instanceof User) {
$userLog = new UserLog();
$userLog->setDescription($entity->getId() . ' being updated.');

$em->persist($userLog);

// Instead of $em->flush() cos we're already in flush process
$userLogMetadata = $em->getClassMetadata(get_class($userLog));
$uow->computeChangeSet($userLogMetadata, $userLog);
}
}
}
}

不工作 preUpdate() 示例

class UserListener
{
public function preUpdate(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
$em = $args->getEntityManager();
$uow = $em->getUnitOfWork();

if ($entity instanceof User) {
$userLog = new UserLog();
$userLog->setDescription($entity->getId() . ') been updated.');

$em = $args->getEntityManager();
$em->persist($userLog);
$userLogMetadata = $em->getClassMetadata(get_class($userLog));
$uow->computeChangeSet($userLogMetadata, $userLog);
}
}
}

最佳答案

解决方案:

诀窍是,在 postFlush() 事件中的 preUpdate() 之后持续存在。

注意:虽然这可能不是最好的解决方案,但它回答了问题,但是可以使用事件订阅者或简单的 onFlush() -> 事件监听器中的 $uow->getScheduledEntityUpdates()

Service.yml

services:

entity.event_listener.user_update:
class: Site\FrontBundle\EventListener\Entity\UserUpdateListener
tags:
- { name: doctrine.event_listener, event: preUpdate }
- { name: doctrine.event_listener, event: postFlush }

事件监听器

<?php

namespace Site\FrontBundle\EventListener\Entity;

use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PostFlushEventArgs;
use Site\FrontBundle\Entity\User;
use Site\FrontBundle\Entity\UserLog;

class UserUpdateListener
{
private $log = array();

public function preUpdate(LifecycleEventArgs $args)
{
$entity = $args->getEntity();

// False check is compulsory otherwise duplication occurs
if (($entity instanceof User) === false) {
$userLog = new UserLog();
$userLog->setDescription($entity->getId() . ' being updated.');

$this->log[] = $userLog;
}
}

public function postFlush(PostFlushEventArgs $args)
{
if (! empty($this->log)) {
$em = $args->getEntityManager();
foreach ($this->log as $log) {
$em->persist($log);
}
$em->flush();
}
}
}

关于symfony - 使用事件监听器创建 preUpdate 或 preFlush 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26439422/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com