【问题标题】:symfony 5 , Doctrine Lifecycle Listeners not runningsymfony 5,Doctrine Lifecycle Listeners 没有运行
【发布时间】:2020-12-22 13:07:58
【问题描述】:

我尝试创建 Doctrine Lifecycle Listeners,但他不执行转储。 我创建了一个实体、一个监听器并更新了 service.yml

这太疯狂了 ;-)

我有: 在 services.yaml 中

  App\EventListener\TestOrmListener:
    tags:
      - { name: doctrineormtester,event: postPersist, entity: 'App\Entity\Galery'}

在 src/EventListener/TestOrmListener.php 中

<?php
namespace App\EventListener;

use App\Entity\Galery;
use Doctrine\Persistence\Event\LifecycleEventArgs;

class TestOrmListener
{
    // the listener methods receive an argument which gives you access to
    // both the entity object of the event and the entity manager itself
    public function postPersist(LifecycleEventArgs $args): void
    {
        $entity = $args->getObject();

        // if this listener only applies to certain entity types,
        // add some code to check the entity type as early as possible
        if (!$entity instanceof Galery) {
            return;
        }

        $entityManager = $args->getObjectManager();
        dump($entityManager);
        die('mick');
        // ... do something with the Product entity
    }
}

实体画廊中的一个

    <?php

namespace App\Entity;

use App\Repository\GaleryRepository;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\Traits\Timestampable;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity(repositoryClass=GaleryRepository::class)
 * @ORM\HasLifecycleCallbacks
 */
class Galery
{
    use Timestampable;
    /**
     * @ORM\Id
     * @Assert\NotBlank
     * @ORM\Column(type="string", length=255)
     */
    private $nom;

你有什么想法吗?

【问题讨论】:

  • 我猜你的听众标签有错别字。
  • 我在 services.yaml 中有错误,但监听器没有运行
  • 尝试修复你的监听器标签定义,它可能有助于触发你的监听器生命周期事件。 - {名称:'doctrine.orm.entity_listener',事件:'postPersist',实体:'App\Entity\Galery'}
  • @eldino 以及它是如何结束的?我的回答有帮助还是您找到了其他解决方案?请为未来的读者留下一些回应。
  • 好的,我写评论,谢谢

标签: php symfony


【解决方案1】:

您的标签部分有问题。 据官方documentation

下一步是在 Symfony 应用程序中启用 Doctrine 侦听器,方法是为它创建一个新服务并使用doctrine.event_listener 标签标记它

所以你的服务定义应该是这样的:

App\EventListener\TestOrmListener:
        tags:
            - { name: 'doctrine.event_listener', event: 'postPersist', entity: 'App\Entity\Galery' }

【讨论】:

  • 我也在我自己的项目(和你的听众)上测试过它并且它有效
  • 感谢您的帮助@Karol Dabrowski
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多