Symonfy 6.4 #[AsEntityListener] PostFlush 不起作用

问题描述 投票:0回答:1

现在使用 symfony 6.4,我试图将我的服务定义(.yml)中的一些内容转换为 php 属性。 我正在做的是删除 EntityListener 的服务定义并将其替换为相应的 PHP 属性。

它适用于某些事件(更新前/更新后)事件,但永远不会调用 postFlush...我做错了什么?

使用前.yml:

    App\Listener\Task\TaskCompletedListener:
        tags:
            - { name: doctrine.event_listener, event: postFlush }
            - { name: doctrine.event_listener, event: preUpdate }

在我的 Listener class.php 中之后

<?php


namespace App\Listener\Task;

use App\Entity\Task\Task;
use App\Entity\Task\TaskUserLog;
use App\Entity\User\User;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
use Doctrine\ORM\Event\PostFlushEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use Doctrine\ORM\Events;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

#[AsEntityListener(event: Events::preUpdate, method: 'preUpdate', entity: Task::class)]
#[AsEntityListener(event: Events::postFlush, method: 'postFlush', entity: Task::class)]
class TaskCompletedListener
{

    private $taskUserLog=null;
  
    private TokenStorageInterface $tokenStorage;

    public function __construct(TokenStorageInterface $tokenStorage)
    {
        $this->tokenStorage = $tokenStorage;
    }

    public function preUpdate(Task $task, PreUpdateEventArgs $args){


        if($args->hasChangedField('lastCompletionDate')){

            //Log new entry to see who did the task
            $taskUserLog=new TaskUserLog();

            /** @var User $user */
            $user=$this->tokenStorage->getToken()->getUser();
            $taskUserLog->setUser($user);
            $taskUserLog->setTask($task);

            $this->taskUserLog=$taskUserLog;
        }
    }

    /**
     * @param Task $task
     * @param PostFlushEventArgs $event
     */
    public function postFlush(Task $task, PostFlushEventArgs $event)
    {

        if(!is_null($this->taskUserLog)) {

            $em = $event->getObjectManager();

            $em->persist($this->taskUserLog);
            $this->taskUserLog=null;
            $em->flush();
        }
    }
}

我认为 symfony 文档足够清晰,更改也很简单,但我不知道为什么它不起作用。每堂课只有一次活动吗?

https://symfony.com/doc/current/doctrine/events.html#:~:text=%24event%20argument).-,Doctrine%20Entity%20Listeners,是%20modified%20in%20the%20database。

更新 这些事件适用于 PreUpdate/PostUpdate,但不适用于 PostFlush...

这是我的学说.yaml

doctrine:
dbal:
    driver:   pdo_mysql
    host:     "%database_host%"
    port:     "%database_port%"
    dbname:   "%database_name%"
    user:     "%database_user%"
    password: "%database_password%"
    charset:  UTF8
orm:
    auto_generate_proxy_classes: "%kernel.debug%"

    entity_managers:
        default:
            mappings:
                App:
                    is_bundle: false
                    type: attribute
                    dir: '%kernel.project_dir%/src/Entity'
                    prefix: 'App\Entity'
                    alias: App
            naming_strategy: doctrine.orm.naming_strategy.underscore
            auto_mapping: true
            query_cache_driver:
                type: service
                id: doctrine.cache.memcached
            result_cache_driver:
                type: service
                id: doctrine.cache.memcached

            filters:
                softdeleteable:
                    class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
                    enabled: true
            dql:
                string_functions:
                    MONTH: DoctrineExtensions\Query\Mysql\Month
                    YEAR: DoctrineExtensions\Query\Mysql\Year
                    DAY: DoctrineExtensions\Query\Mysql\Day
                    DATE: DoctrineExtensions\Query\Mysql\Date
                    GROUP_CONCAT: DoctrineExtensions\Query\Mysql\GroupConcat
symfony events doctrine
1个回答
0
投票

单独调用postFlush事件而不调用preUpdate是否被调用?

您还可以在

doctrine:orm:
文件下显示
config/packages/doctrine.yaml
设置吗?谢谢

© www.soinside.com 2019 - 2024. All rights reserved.