-1

How can I inject a service into the current user object?

With current user object I mean this:

//in controller
$user = $this->get('security.token_storage')->getToken()->getUser();

//later I want to do this in the user class: 
function getData() {
   $data = $this->getSomeData();//normal function
   $data += $this->getMyService()->getSomeMoreData();//invoke service function
}
musicman
  • 504
  • 6
  • 16

1 Answers1

1

This is very bad practice as your Entity classes should be simple objects that are responsible only for manipulating their own data.

If, however, you are determined to do this you can create the container property in your User class and inject it in the Doctrine postLoad event, see the documentation.

I would stress again that you shouldn't do this though. I've seen this done before and it leads to over-complex classes that can have multiple responsibilities and are impossible to test.

mickadoo
  • 3,337
  • 1
  • 25
  • 38