Symfony2 Event_listener未触发

时间:2012-03-13 23:00:14

标签: events symfony doctrine-orm

我正在使用Symfont RC5,我正试图让事件监听器prePersist解雇。

app/config/config.yml我有: -

services:
   recordmanagement:
      class: FNQITServices\BMBundle\Listener\RecordManagement
      tags:
        - { name: doctrine.event_listener, event: prePersist }

我创建了一个班级src/FNQITServices/BMBundle/Listener/RecordManagement.php: -

<?php
namespace FNQITServices\BMBundle\Listener;

use Doctrine\ORM\Event\LifecycleEventArgs;
use FNQITServices\BMBundle\Entity\Elections;
use FNQITServices\BMBundle\Entity\People;

class RecordManagement
{
    public function prePersist(LifecycleEventArgs $args)
    {
        `echo "amibeingcalled" > /tmp/test`;

        $entity = $args->getEntity();
        $entityManager = $args->getEntityManager();

        // perhaps you only want to act on some "Product" entity
        if ($entity instanceof Elections) {
            $person = $this->getDoctrine()
                       ->getRepository('FNQITServicesBMBundle:People')
                       ->find(2);

            $entity->setCreator($person);
        }
    }
}

但是在插入过程中事件没有被触发。即没有创建文件/tmp/test

  • 我注意到有kernel.event_listener我注意到这曾经是kernel.listener几个版本的回复
  • 我在配置中尝试过各种'标签'属性组合。
  • 我禁用了选举实体中的生命周期注释(如果存在冲突)
  • 我查看了控制台日志,事件......试图找到问题的任何提及

我正在使用索纳塔的CRUD。不确定是否会产生任何差异。

4 个答案:

答案 0 :(得分:0)

尝试

services:
recordmanagement:
    class: FNQITServices\BMBundle\Listener\RecordManagement
    tags:
        - { name: doctrine.event_listener, event: prePersist, method: testMethod }

<?php
    namespace FNQITServices\BMBundle\Listener;

    use Doctrine\ORM\Event\LifecycleEventArgs;
    use FNQITServices\BMBundle\Entity\Elections;
    use FNQITServices\BMBundle\Entity\People;

    class RecordManagement
    {
        public function testMethod(LifecycleEventArgs $args)
        {
            ...
        }
    }

答案 1 :(得分:0)

也许这不是问题,但你使用的是非常古老的symfony版本。也许首先尝试升级到最新稳定(现在2.0.12)

答案 2 :(得分:0)

遇到同样的问题。

只是用Symfony 2.0.12(当前版本)和奏鸣曲来测试它,它似乎不起作用。我现在正在使用onFlush Event来满足我的需求。

似乎SonataDoctrineORMAdmin没有计算Changeset权限,因为每个Field都会一直更新,所以你必须手动检查。

答案 3 :(得分:0)

我在src / Acme / BMBundle / Resources / config中创建了services.xml,如下所示: -

<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>
        <service id="acme_bm.listener" class="ACME\BMBundle\Listener\RecordManagement">
            <argument type="service" id="service_container" />
            <tag name="doctrine.event_listener" event="prePersist" method="prePersist"/>
        </service>
        <service id="acme_bm.listener2" class="ACME\BMBundle\Listener\RecordManagement">
            <argument type="service" id="service_container" />
            <tag name="doctrine.event_listener" event="preUpdate" method="preUpdate"/>
        </service>
    </services>
</container>

然后我按如下方式创建了Listener / RecordManagement.php: -

<?php
namespace ACME\BMBundle\Listener;

use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use Symfony\Component\DependencyInjection\ContainerInterface; 

class RecordManagement 
{
    protected $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container; 
    }


    public function prePersist(LifecycleEventArgs $args)
    {
.
.
.
    }
.
.
.
.
}