Le 12/05/2011 09:16, Valera a écrit :
Hi,
I'd like to make use of @orm:postPersist/postUpdate/postRemove
features to perform audit logging ("who" and "when" has done "what" to
a Doctrine-powered entity) into DB.
The audit code is located in a service, say \My\TestBundle\Service
\AuditService.php.
The problem I'm facing is how to inject (and whether to inject at
all?) the AuditService in entity classes. The only option I see for
now is quite ugly and I really hope to see if there are more graceful
solutions:
Add the following code into MyTestBundle class:
private static $containerInstance = null;
public function setContainer(\Symfony\Component\DependencyInjection
\ContainerInterface $container = null) {
parent::setContainer($container);
self::$containerInstance = $container;
}
public static function getContainer() {
return self::$containerInstance;
}
And later on, in the @orm:postPersist/.... methods of my entities
classes, make calls like MyTestBundle::getContainer()->get('my.audit')-
audit($this);
A lifecycle callback has only access to the current entity. To be able
to use a service, you need to use an event listener instead :
http://www.doctrine-project.org/docs/orm/2.0/en/reference/events.html#implementing-event-listeners
for the way it works and
http://symfony.com/doc/2.0/book/doctrine/orm.html#registering-event-listeners-and-subscribers
for the way to register the Doctrine listeners in a Symfony2 project.
--
Christophe | Stof
--
If you want to report a vulnerability issue on symfony, please send it to
security at symfony-project.com
You received this message because you are subscribed to the Google
Groups "symfony users" group.
To post to this group, send email to symfony-users@googlegroups.com
To unsubscribe from this group, send email to
symfony-users+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en