1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
//we get the old picture for delete later///
// we need to do that before the handleRequest because the user entity is linked to the form and the repository keep it in cache///
// and doesn't go in the db to look for it.///
// the best solution is not mapping forms to entities and change every changes manually.///
//TODO : need to update and put it in the condition where the form is submitted
$oldUser = $em->getRepository('BaseBundle:User')->find($user->getId());
$oldFile = $oldUser->getPicture();
$editForm->handleRequest($request);
///IF FORM IS SUBMITTED WE TREAT DATAS///
if ($editForm->isSubmitted() && $editForm->isValid()) {
///WE UPDATE PICTURE IF THERE IS NEED IT AND DELETE THE OLD ONE///
if($user->getPicture()==null)
{
$user->setPicture($oldFile);
}
else
{
if($oldFile!='default.jpg')
unlink($this->getParameter('user_picture_directory').$oldFile);
/** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */
$file = $user->getPicture();
$fileName = md5(uniqid()) . '.' . $file->guessExtension();
$file->move(
$this->getParameter('user_picture_directory'),
$fileName
);
$user->setPicture($fileName);
}
///WE SET DATAS IN DB |
Partager