I need to write the name of logged user in some fields of my model (I've got a parent document and some embedded documents)
I've injected security context in my admin class
calls:
- [ setTranslationDomain, [QMAdminBundle]]
- [ setSecurityContext, [@security.context]]
then I wrote my setSecurityContext
public function setSecurityContext($securityContext) {
$this->securityContext = $securityContext;
}
public function getSecurityContext() {
return $this->securityContext;
}
public function prePersist($appunto) {
$user = $this->getSecurityContext()->getToken()->getUser();
$appunto->setOperatore($user->getUsername());
}
Unfortunately it doesn't write the user... maybe this happens because I've got a parent document (where I don't need to write the user) and some embedded documents (where I want to write the user) that I show in the parent form (please see image)?
I use fosuserbundle, and a bundle that (easy-)extends sonata user: I have to use them to get the user?
Do you have any suggestions to solve this issue?
Thanks a lot
If you write
$appunto->setOperatore($user->getUsername());
in the prePersist
function, it'll save it once you've pressed the save button, right before it's persisted in memory.
Try to set the operator on the appunto object in the __constructor()
function, and it'll show the username in the appunto
object.
Remember that you'll have to set the user for each subobjects
like $appunto->getSubObject()->setOperatore($user);