Skip to content
Advertisement

Symfony get connected user id from entity

In a Symfony2.8/Doctrine2 application, I need to store in each row of my SQL tables the id of the user who created or updated the row (users can connect with Ldap).

So all my entities inherited of a GenericEntity which contains this variable (type would be string if I want to store Ldap username):

JavaScript

And I use the prePersistCallback() to automatically assign this value:

JavaScript

But I don’t know how to retrieve the connected user, or how to automatically inject it in the entity… How can I do it?

Advertisement

Answer

You can use a Doctrine entity listener/subscriber instead to inject the security token and get the current logged user:

JavaScript

Next register your listener:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement