坚持onFlush监听器symfony

时间:2018-09-19 07:58:15

标签: symfony listener persist

我正在尝试在onFlush侦听器中保留一个实体... 这是我的代码:

    public function onFlush(OnFlushEventArgs $args){

    $em = $args->getEntityManager();
    $uow = $em->getUnitOfWork();

    foreach ($uow->getScheduledEntityUpdates() as $entity) {
        switch (true) {
            case $entity instanceof Products:

                $changeset = $uow->getEntityChangeSet($entity);

                foreach($changeset as $column => $change) {
                            $logs = new Logs();
                            $logs->setDate(new DateTime());


                            $em->persist($logs);
                            $classMetadata = $em->getClassMetadata('BddBundle:Logs');
                            $uow->computeChangeSet($classMetadata, $logs);
                        }
                    }
                }
                break;
            default : break;
        }
    }
}

问题是程序块(没有行插入数据库)和PHP都说:

PHP致命错误:在第653行的applis / vendor / doctrine / orm / lib / Doctrine / ORM / UnitOfWork.php中超过了300秒的最大执行时间,引用者:http://website/

有人有解决方案吗?

致谢

编辑1:

将$ em更改为$ uow不会做任何更改...

             $uow->persist($logs);
                    $classMetadata = $em->getClassMetadata('BddBundle:Logs');
                    $uow->computeChangeSet($classMetadata, $logs);

1 个答案:

答案 0 :(得分:1)

这会监视User实体上的更改,并将少量数据插入UserLog实体中。根据您的需要进行调整。

use Doctrine\ORM\Event\OnFlushEventArgs;

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

        foreach ($uow->getScheduledEntityUpdates() as $entity) {
            if ($entity instanceof User) {
                $userLog = new UserLog();
                $userLog->setUserId($entity->getId());
                $userLog->setMessage('onFlush - update at ' . date('d/m/Y H:i:s'));

                $em->persist($userLog);
                // Instead of $em->flush() because we are already in flush process
                $uow->computeChangeSet($em->getClassMetadata(get_class($userLog)), $userLog);
            }
        }
    }
}