I have followed this to add the functionality to upload files, but I have a little problem. I actually copied the functions from the section Using Lifecycle Callbacks. Instead of class Document, I have a class Friend with this:
//...
/**
* @Assert\File(maxSize="6000000")
*/
public $picture;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $path;
//...
The upload and edit work, but I have problem when deleting. I want to have two options – Remove the picture and Delete the picture– the first will only set the current path to null and the file will remain in the folder where the files are stored while the second will set to path to null and will delete the file, too.
The bad news is that I cannot make the path to be null. The file is deleted, but the path stays.
This is the function from the documentation:
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if ($picture = $this->getAbsolutePath()) {
unlink($picture);
}
}
public function getAbsolutePath()
{
return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->path;
}
and this is the action in my controller:
public function removePictureAction($id)
{
$em = $this->getDoctrine()->getEntityManager();
$friend = $em->getRepository('EMMyFriendsBundle:Friend')->find($id);
$friend->removeUpload();
$var=null;
$friend->setPath($var);
return $this->redirect($this->generateUrl('friend_id', array('id' => $id)));
}
but the path stays… How to delete it and set it to be null again?
You need to persist your friend entity and flush the entity manager