set user authenticated on Admin field doesn't work!

29 views
Skip to first unread message

Edi Camas

unread,
Jan 26, 2017, 6:31:07 PM1/26/17
to sonata-devs
Hello, i need some help. I need to persist the user authenticated to a field in an admin. I tried this:

public function prePersist($object){
        $object->setFecha(new \DateTime());
        $object->setIdResponsable($this->getConfigurationPool()->getContainer()->get('security.token_storage')->getToken()->getUser());
}

the error is the next:

Catchable Fatal Error: Argument 1 passed to AppBundle\Entity\SolicitudesServicio::setIdResponsable() must be an instance of AppBundle\Entity\FosUserUser, instance of Application\Sonata\UserBundle\Entity\User given, called in C:\wamp\www\gspcorp\src\AppBundle\Admin\SolicitudesServicioAdmin.php on line 89 and defined


Someone know how to solve it?.

olemedia

unread,
Jul 23, 2017, 2:00:02 PM7/23/17
to sonata-devs
I had to do something similar, what I did is:
1. I created a controller that returns the expected value
2. I created a service so that I can access that controller from anywhere
3. I call the service from within sonata, something like this:

$usrSrvc = $this->getConfigurationPool()->getContainer()->get('app.user_manager');
$user = $usrSrvc->getLoggedUser();

Now, remember that in your entity you need the setter and getter in order to set your data.

Hope this helps

L.
Reply all
Reply to author
Forward
0 new messages