File 从FosUserBundle编辑用户实体上的字段文件
我有一个用户实体 User.php 在控制器中,我有File 从FosUserBundle编辑用户实体上的字段文件,file,symfony,photo,fosuserbundle,File,Symfony,Photo,Fosuserbundle,我有一个用户实体 User.php 在控制器中,我有 public function deleteAction($id) { $em= $this->getDoctrine()->getManager(); $pers= $em->getRepository('BackUserBundle:User')->find($id); $tmp = $pers->getFile(); if($tmp != NULL) {
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'));
}
它工作正常,删除文件并清除路径字段
我现在的问题是创建一个函数来更改路径字段和文件
有人帮我吗
谢谢你:)你需要的东西差不多都有了,我想,按照官方的文件: 在控制器中:
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());
}
移动文件后,必须稍微更改实体中的上载功能以保存文件路径
User.php
不要忘记上传操作的路由
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();