如何用Symfony 2.4使用Doctrine Entity Listener? [英] How to use Doctrine Entity Listener with Symfony 2.4?

查看:95
本文介绍了如何用Symfony 2.4使用Doctrine Entity Listener?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Doctrine Entity Listener with Symfony 2.4。

I want to use Doctrine Entity Listener with Symfony 2.4.

我创建了一个类:

<?php

namespace MyBundle\Listener\Entity;

use Doctrine\ORM\Event\LifecycleEventArgs;
use MyBundle\Entity\User;

class UserListener
{
    public function postPersist(User $user, LifecycleEventArgs $event)
    {
    }
}

我添加了地图信息:

<?xml version="1.0" encoding="UTF-8" ?>
<doctrine-mapping>
    <entity name="MyBundle\Entity\User">

        ...

        <entity-listeners>
            <entity-listener class="MyBundle\Listener\Entity\UserListener" />
        </entity-listeners>

    </entity>
</doctrine-mapping>

它可以正常工作。但是如何通过一些服务到这个班?我想要将其与Symfony的服务容器集成并提供服务。

It's working OK. But how do I pass some services to this class? I want it to be integrated with the Symfony's service container and provided services.

推荐答案

要在symfony中注册一个特定的实体侦听器实例请按照下列步骤操作:

To register a specific entity listener instance in symfony please follow this steps:

1)将composer.json中的DoctrineBundle更新到1.3版本(因为它还不稳定):

1) Update DoctrineBundle in your composer.json to the version 1.3 (since it's not yet stable):

{
    "require": {
        "doctrine/doctrine-bundle": "1.3.x-dev",
    }
}

2)使用标签名称doctrine.orm.entity_listener注册自己的服务:

2) Register your own service with tag name "doctrine.orm.entity_listener":

my.listener:
    class: MyBundle\Listener\Entity\UserListener
    arguments: [ ... ]
    tags:
        - { name: doctrine.orm.entity_listener }

请参阅实体侦听器上的Symfony文档

这篇关于如何用Symfony 2.4使用Doctrine Entity Listener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆