Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 从FosUserBundle编辑用户实体上的字段文件_File_Symfony_Photo_Fosuserbundle - Fatal编程技术网

File 从FosUserBundle编辑用户实体上的字段文件

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) {

我有一个用户实体

User.php

在控制器中,我有

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();