How to Get Current User inside Repository Symfony 2.7 -
all of query in entity repository needs filtered user.
now want know how can access logged in user in entity repository directly.
what did today logged in user in controller, through use of $this->getuser() , pass entity repository , not efficient.
you need inject security.token_storage
service 1 current user, of repository classes belong doctrine project, not symfony, not recommended this.. may there way achieve creating custom entitymanager class described here, don't think solution..
instead of customizing entitymanager
better create service calls repository classes' methods, inject desired services it.. let repository classes job.
implementation this:
repositoryclass:
class myrepository extends entityrepository { public function fetchsomedatabyuser(userinterface $user) { // query } }
service:
class myservice { private $tokenstorage; public function _construct(tokenstorageinterface $tokenstorage) { $this->tokenstorage = $tokenstorage; // other services } public function getsomedatabyuser() { $user = $this->tokenstorage->gettoken()->getuser(); return $this->entitymanager->getrepository(myrepository)->fetchsomedatabyuser($user); } }
usage:
public function someaction() { $databyuser = $this->get(myservice)->getsomedatabyuser(); }
Comments
Post a Comment