In a Symfony2.8/Doctrine2 application, I need to store in each row of my SQL tables the id of the user who created or updated the row (users can connect with Ldap).
So all my entities inherited of a GenericEntity
wich contains this variable (type would be string if I want to store Ldap username):
/**
* @var integer
*
* @ORM\Column(name="zzCreationId", type="string", nullable=false)
*/
private $creationId;
And I use the prePersistCallback()
to automatically assign this value:
/**
* @ORM\PrePersist
*/
public function prePersistCallback()
{
$currentUser = /* ...... ????? ....... */ ;
if ($currentUser->getId() != null) {
$this->creationId = $currentUser->getId() ;
} else {
$this->creationId = 'unknown' ;
}
return $this;
}
But I don't know how to retreive the connected user, or how to automatically inject it in the entity... How can I do it?