我有用户实体
user.php的
namespace Back\UserBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Security\Core\Util\SecureRandom;
/**
* @ORM\Entity()
* @ORM\HasLifecycleCallbacks()
* @ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=255)
*
*/
private $first_name;
/**
* @ORM\Column(type="string", length=255)
*
*/
private $last_name;
/**
* @ORM\Column(type="string", length=255)
*
*/
private $genre;
/**
* @ORM\Column(type="date")
*
*/
private $date_naissance;
/**
* @ORM\Column(type="string", length=255)
*
*/
private $pays;
/** @ORM\Column(type="datetime") */
private $dateinscription;
public function __construct()
{
parent::__construct();
$this->dateinscription = new \DateTime();
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param mixed $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return mixed
*/
public function getLastName()
{
return $this->last_name;
}
/**
* @param mixed $last_name
*/
public function setLastName($last_name)
{
$this->last_name = $last_name;
}
/**
* @return mixed
*/
public function getGenre()
{
return $this->genre;
}
/**
* @param mixed $genre
*/
public function setGenre($genre)
{
$this->genre = $genre;
}
/**
* @return mixed
*/
public function getFirstName()
{
return $this->first_name;
}
/**
* @param mixed $first_name
*/
public function setFirstName($first_name)
{
$this->first_name = $first_name;
}
/**
* @return mixed
*/
public function getPays()
{
return $this->pays;
}
/**
* @param mixed $pays
*/
public function setPays($pays)
{
$this->pays = $pays;
}
/**
* @return mixed
*/
public function getDateNaissance()
{
return $this->date_naissance;
}
/**
* @param mixed $date_naissance
*/
public function setDateNaissance($date_naissance)
{
$this->date_naissance = $date_naissance;
}
/**
* @return mixed
*/
public function getDateinscription()
{
return $this->dateinscription;
}
/**
* @param mixed $dateinscription
*/
public function setDateinscription($dateinscription)
{
$this->dateinscription = $dateinscription;
}
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $path;
/**
* @Assert\File(maxSize="6000000")
*/
private $file;
private $temp;
/**
* @return mixed
*/
public function getFile()
{
return $this->file;
}
/**
* @return mixed
*/
public function getPath()
{
return $this->path;
}
/**
* @param mixed $path
*/
public function setPath($path)
{
$this->path = $path;
}
/**
* @param mixed $path
*/
public function setNullPath()
{
$this->path = NULL;
}
/**
* Get root directory for file uploads
*
* @return string
*/
protected function getUploadRootDir($type='profilePicture') {
// the absolute directory path where uploaded
// documents should be saved
return __DIR__.'/../../../../web/'.$this->getUploadDir($type);
}
/**
* Specifies where in the /web directory profile pic uploads are stored
*
* @return string
*/
protected function getUploadDir($type='profilePicture') {
// the type param is to change these methods at a later date for more file uploads
// get rid of the __DIR__ so it doesn't screw up
// when displaying uploaded doc/image in the view.
return 'uploads/user/profilepics';
}
/**
* Sets file.
*
* @param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
// check if we have an old image path
if (is_file($this->getAbsolutePath())) {
// store the old name to delete after the update
$this->temp = $this->getAbsolutePath();
} else {
$this->path = NULL;
}
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (null !== $this->getFile()) {
$this->path = $this->getFile()->guessExtension();
}
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->getFile()) {
return;
}
// check if we have an old image
if (isset($this->temp)) {
// delete the old image
unlink($this->temp);
// clear the temp image path
$this->temp = null;
}
// you must throw an exception here if the file cannot be moved
// so that the entity is not persisted to the database
// which the UploadedFile move() method does
$this->getFile()->move(
$this->getUploadRootDir(),
$this->id.'.'.$this->getFile()->guessExtension()
);
$this->setFile(null);
}
/**
* @ORM\PreRemove()
*/
public function storeFilenameForRemove()
{
$this->temp = $this->getAbsolutePath();
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if (isset($this->temp)) {
unlink($this->temp);
}
}
public function getAbsolutePath()
{
return null === $this->path
? null
: $this->getUploadRootDir().'/'.$this->id.'.'.$this->path;
}
public function removeFile()
{
$tmp = $this->getUploadRootDir().'/'.$this->id.'.'.$this->path;
unlink($tmp);
}
}
在控制器中我有
public function deleteAction($id)
{
$em= $this->getDoctrine()->getManager();
$pers= $em->getRepository('BackUserBundle:User')->find($id);
$tmp = $pers->getFile();
if($tmp != NULL)
{
$pers->removeFile();
$pers->setFile();
$em->persist($pers);
$em->flush();
}
return new RedirectResponse($this->get('router')->generate('fos_user_profile_show'));
}
它正常工作,删除文件并清除路径字段。
我现在的问题是创建一个更改路径字段和文件的函数。
有人帮助我吗? 谢谢你:)
答案 0 :(得分:0)
我认为你几乎拥有所需的一切,请遵循官方文件:
http://symfony.com/fr/doc/current/cookbook/doctrine/file_uploads.html
在您的控制器中:
public function uploadAction()
{
$user= new User();
$form = $this->createFormBuilder($user)
->add('file')
->getForm()
;
if ($this->getRequest()->isMethod('POST')) {
$form->handleRequest($this->getRequest());
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$user->upload();
$em->persist($user);
$em->flush();
$this->redirect($this->generateUrl(...));
}
}
return array('form' => $form->createView());
}
您必须稍微更改实体中的上传功能,以便在移动后保存文件的路径。
$this->getFile()->move(
$this->getUploadRootDir(),
$this->id.'.'.$this->getFile()->guessExtension()
);
$this->path = $this->getUploadRootDir(),
$this->id.'.'.$this->getFile()->guessExtension();
并且不要忘记上传操作的路由。