1

Let's assume, that I have two entity classes:
\AppBundle\Entity\User - user provider;
\AppBundle\Entity\Article - simple article.

The Article class also have these properties:
author - indicating on user which created this particular entity;
updatedBy - indicating on user which lately updated content of particular article.

How to pass currently logged user object to Article entity to set specific values on author and/or updatedBy properties on backend generated by EasyAdminBundle on Symfony 3.0.1?

Jazi
  • 6,569
  • 13
  • 60
  • 92
  • Why did you tag your question with `symfony2`, when it is explicitly about `symfony3`? – cezar Apr 12 '16 at 18:40
  • @cezar Still very small amount of people is using `symfony3` tag and Symfony 3 is not really so much different from Symfony 2.8. – Jazi Apr 12 '16 at 18:49

2 Answers2

2

If you are in a Controller you just do that

$article->setAuthor($this->getUser());
$article->setUpdatedBy($this->getUser());

Or

If you want that this is automatic

You need to declare a listener on Doctrine event. In your case I guess on preUpdate by including the current user.

There is a very nice documentation here http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html

I edit here to answer at your comment

Don't worry you can inject the user entity when you declare your listener as Service

For example :

services:
    your_listener:
        class:     App\AppBundle\Your_Listener
        arguments: ["@security.token_storage"]

Your listener :

private $current_user;

public function __construct($security_context) {
        if ($security_context->getToken() != null) {
            $this->current_user = $security_context->getToken()->getUser();
        }
    } 

Now you can do

$entity= $args->getEntity(); // get your Article

if (!$entity instanceof Article) {
        return;
}

$entity->setAuthor($this->current_user);
$entity->setUpdatedBy($this->current_user);
François Dupont
  • 406
  • 1
  • 4
  • 19
  • I would like to take 2nd approach. How to include current user object instance to my listener class? – Jazi Apr 11 '16 at 07:36
  • Look at the documentation You need this as parameter : LifecycleEventArgs $args to get the user entity like this : $entity = $args->getEntity(); Just here in the doc : http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html#creating-the-listener-class Hope this is can help you – François Dupont Apr 11 '16 at 07:41
  • Yeah, but when I'm updating `Article` entity, I will get this class instance in `$args->getEntity();` and not a User. I want to set current logged user as author of this particular entity. – Jazi Apr 11 '16 at 07:57
  • 1
    I changed my answer to answer you – François Dupont Apr 11 '16 at 08:05
  • Thanks! That helped me a lot! But two things about Your answer: 1. Add proper tag to service :). 2. Token check should be in other listener method (not in constructor). For example `preUpdate()`. In constructor, I think, there should be just assigning content to a private value, because the token in constructor is `null` (from http://stackoverflow.com/questions/32590621/tokenstorage-sometimes-returns-null-in-service). – Jazi Apr 11 '16 at 08:21
  • I had never heard about tag so thanks too, I will see that ! Yes it's maybe better to have a service which load the user and use tag when you need it – François Dupont Apr 11 '16 at 08:29
  • As documentation says, service should have "tags: - { name: doctrine.event_listener, event: postPersist }" – Jazi Apr 11 '16 at 08:30
  • Did you use the Symfony security system when you log ? If no this is normal that you get a null user. – François Dupont Apr 11 '16 at 08:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108791/discussion-between-francois-dupont-and-krzysztof-trzos). – François Dupont Apr 11 '16 at 08:34
0

If you have relations to Author entity

 $em = $this->getDoctrine()->getManager();
$user = new User();
$user->setAuthor($this->getUser());
$em->persist($user);
$em->flush();

on controller but that is bad idea. Use Command or another design pattern.

M4ver
  • 51
  • 7