如何在 Symfony 2.4 中使用 Doctrine 实体监听器? [英] How to use Doctrine Entity Listener with Symfony 2.4?
问题描述
我想使用 使用 Symfony 2.4 的 Doctrine 实体监听器.
我创建了一个类:
我添加了映射信息:
一切正常.但是我如何将一些服务传递给这个类呢?我希望它与 Symfony 的服务容器集成并提供服务.
要在 symfony 中注册特定的实体监听器实例,请按照以下步骤操作:
1) 将 composer.json 中的 DoctrineBundle 更新到 1.3 版(因为它还不稳定):
<代码>{要求": {"doctrine/doctrine-bundle": "1.3.x-dev",}}
2) 使用标签名称doctrine.orm.entity_listener"注册您自己的服务:
my.listener:类:MyBundleListenerEntityUserListener参数: [ ... ]标签:- { 名称:doctrine.orm.entity_listener }
请参阅 有关实体侦听器的 Symfony 文档..>
I want to use Doctrine Entity Listener with Symfony 2.4.
I've created a class:
<?php
namespace MyBundleListenerEntity;
use DoctrineORMEventLifecycleEventArgs;
use MyBundleEntityUser;
class UserListener
{
public function postPersist(User $user, LifecycleEventArgs $event)
{
}
}
I've added mapping information:
<?xml version="1.0" encoding="UTF-8" ?>
<doctrine-mapping>
<entity name="MyBundleEntityUser">
...
<entity-listeners>
<entity-listener class="MyBundleListenerEntityUserListener" />
</entity-listeners>
</entity>
</doctrine-mapping>
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.
To register a specific entity listener instance in symfony please follow this steps:
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) Register your own service with tag name "doctrine.orm.entity_listener":
my.listener:
class: MyBundleListenerEntityUserListener
arguments: [ ... ]
tags:
- { name: doctrine.orm.entity_listener }
See the Symfony docs on Entity Listeners.
这篇关于如何在 Symfony 2.4 中使用 Doctrine 实体监听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!