代码之家  ›  专栏  ›  技术社区  ›  Lukaszy

使用文件上载编辑表单后,文件“”不存在

  •  1
  • Lukaszy  · 技术社区  · 8 年前

    我正在尝试为我的任务实体上载文件。我使用两个来源: http://symfony.com/doc/3.4/controller/upload_file.html Symfony2 file upload step by step 但我不知道如何在编辑过程中保持上传的文件。

    我不确定我是否正确实现了该部分: enter image description here

    当我试图编辑任何任务时,我的表单都在抱怨:

    表单的视图数据应为类的实例 Symfony\Component\HttpFoundation\File\File,但为(n)字符串。你 可以通过将“data\u class”选项设置为null或 添加将(n)字符串转换为实例的视图转换器 Symfony \组件\ HttpFoundation \文件\文件的。

    所以我修改了getter getBrochure() 在我要展开的PDF文件的任务实体中。请参见下面我的GetBroadcase方法这是我的实体代码:

    <?php
    
    namespace AppBundle\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    use Symfony\Component\Validator\Constraints as Assert;
    use Symfony\Component\HttpFoundation\File\File;
    
    /**
     * Task
     *
     * @ORM\Table(name="task")
     * @ORM\Entity(repositoryClass="AppBundle\Repository\TaskRepository")
     */
    class Task
    {
        /**
         * @var int
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
    
        /**
         * @var string
         *
         * @ORM\Column(name="name", type="string", length=255)
         */
        private $name;
    
        /**
         * @var \DateTime
         *
         * @ORM\Column(name="datetime", type="datetime")
         */
        private $datetime;
    
        /**
         * @ORM\ManyToMany(targetEntity="Category", inversedBy="tasks")
         * @ORM\JoinTable(name="categories_tasks")
         */
        private $categories;
    
        /**
         * @ORM\Column(type="text")
         */
        private $description;
    
        /**
         * @ORM\Column(type="string")
         *
         * @Assert\File(mimeTypes={ "application/pdf" })
         */
         private $brochure;
    
    
        public function __construct() {
            $this->categories = new \Doctrine\Common\Collections\ArrayCollection();
        }
    
        /**
         * Get id
         *
         * @return int
         */
        public function getId()
        {
            return $this->id;
        }
    
        /**
         * Set name
         *
         * @param string $name
         *
         * @return Task
         */
        public function setName($name)
        {
            $this->name = $name;
    
            return $this;
        }
    
        /**
         * Get name
         *
         * @return string
         */
        public function getName()
        {
            return $this->name;
        }
    
        /**
         * Set datetime
         *
         * @param \DateTime $datetime
         *
         * @return Task
         */
        public function setDatetime($datetime)
        {
            $this->datetime = $datetime;
    
            return $this;
        }
    
        /**
         * Get datetime
         *
         * @return \DateTime
         */
        public function getDatetime()
        {
            return $this->datetime;
        }
    
        public function getCategories()
        {
            return $this->categories;
        }
    
        public function setCategories(Category $categories)
        {
            $this->categories = $categories;
        }
    
        public function getDescription()
        {
            return $this->description;
        }
    
        public function setDescription($description)
        {
            $this->description = $description;
        }
    
        public function getBrochure()
        {
            //return $this->brochure;
            return new File($this->brochure);
        }
    
        public function setBrochure($brochure)
        {
            $this->brochure = $brochure;
    
            return $this;
        }
    
        public function __toString() {
            return $this->name;
        }
    }
    
    ?>
    

    结果是我可以加载编辑页面,但文件上载字段为空,没有我上载任何文件的信息。我不确定是否应该有任何信息,但我在数据库中看到文件名在那里,在web文件夹中也有上载的文件。当我更改任务中的任何内容并清除文件保存时,当我尝试启动编辑页面时,我看到:

    文件“”不存在

    我很清楚,因为此任务的文件列已被清除。所以,当我不想上传新文件时,如何在编辑过程中保留文件?

    这是我的任务类型

    <?php
    
    namespace AppBundle\Form;
    
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolver;
    use Ivory\CKEditorBundle\Form\Type\CKEditorType;
    use Symfony\Component\Form\Extension\Core\Type\FileType;
    
    class TaskType extends AbstractType
    {
        /**
         * {@inheritdoc}
         */
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder->add('name')->add('datetime')->add('categories')
                ->add('description', 'Ivory\CKEditorBundle\Form\Type\CKEditorType', array())
                ->add('brochure', FileType::class, array('label' => 'Broszurka (PDF)', 'required' => false));
        }
    
        /**
         * {@inheritdoc}
         */
        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'AppBundle\Entity\Task'
            ));
        }
    
        /**
         * {@inheritdoc}
         */
        public function getBlockPrefix()
        {
            return 'appbundle_task';
        }
    
    
    }
    

    这是我的TaskController(仅编辑操作)

    /**
     * Displays a form to edit an existing task entity.
     *
     * @Route("/{id}/edit", name="task_edit")
     * @Method({"GET", "POST"})
     */
    public function editAction(Request $request, Task $task)
    {
        $deleteForm = $this->createDeleteForm($task);
        $editForm = $this->createForm('AppBundle\Form\TaskType', $task);
        $editForm->handleRequest($request);
    
        if ($editForm->isSubmitted() && $editForm->isValid()) {
            $this->getDoctrine()->getManager()->flush();
    
            return $this->redirectToRoute('task_edit', array('id' => $task->getId()));
        }
    
        return $this->render('task/edit.html.twig', array(
            'task' => $task,
            'edit_form' => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }
    

    任务实体:

    <?php
    
    命名空间AppBundle \实体;
    
    使用条令\ ORM \映射作为ORM;
    使用Symfony\Component\Validator\Constraints作为断言;
    使用Symfony\Component\HttpFoundation\File\File;
    
    /**
    *任务
    *
    *@ORM\Table(name=“task”)
    *@ORM\Entity(repositoryClass=“AppBundle\Repository\TaskRepository”)
    */
    类任务
    {
    /**
    *@变量int
    *
    *@ORM\Column(name=“id”,type=“integer”)
    *@ORM\Id
    *@ORM\GeneratedValue(策略=“自动”)
    */
    私人$id;
    
    /**
    *@变量字符串
    *
    *@ORM\Column(name=“name”,type=“string”,长度=255)
    */
    私有$名称;
    
    /**
    *@var\DateTime
    *
    *@ORM\Column(name=“datetime”,type=“datetime”)
    */
    私有$datetime;
    
    /**
    *@ORM\ManyToMany(targetEntity=“Category”,inversedBy=“tasks”)
    *@ORM\JoinTable(name=“categories\u tasks”)
    */
    私人$类别;
    
    /**
    *@ORM\Column(type=“text”)
    */
    私人$说明;
    
    /**
    *@ORM\Column(type=“string”)
    *
    *@Assert\File(mimeTypes={“application/pdf”})
    */
    私人$小册子;
    
    
    公共函数\uu construct(){
    $此->类别=新建\条令\通用\集合\阵列集合();
    }
    
    /**
    *获取id
    *
    *@返回整数
    */
    公共函数getId()
    {
    返回$此->身份证件;
    }
    
    /**
    *集合名称
    *
    *@参数字符串$名称
    *
    *@返回任务
    */
    公共函数setName($名称)
    {
    $此->名称=$名称;
    
    返回$this;
    }
    
    /**
    *获取名称
    *
    *@返回字符串
    */
    公共函数getName()
    {
    返回$此->名称
    }
    
    /**
    *设置日期时间
    *
    *@参数\日期时间$日期时间
    *
    *@返回任务
    */
    公共函数setDatetime($datetime)
    {
    $此->datetime=$datetime;
    
    返回$this;
    }
    
    /**
    *获取日期时间
    *
    *@返回\日期时间
    */
    公共函数getDatetime()
    {
    返回$此->日期时间;
    }
    
    公共函数getCategories()
    {
    返回$此->类别;
    }
    
    公共函数集合类别(类别$类别)
    {
    $此->类别=$类别;
    }
    
    公共函数getDescription()
    {
    返回$此->描述
    }
    
    公共函数setDescription($说明)
    {
    $此->描述=$描述;
    }
    
    公共函数get宣传册()
    {
    //返回$此->小册子
    返回新文件($本手册->);
    }
    
    公共功能设置(小册子)
    {
    $此->宣传册=$宣传册;
    
    返回$this;
    }
    
    公共函数\uu toString(){
    返回$此->名称
    }
    }
    
    ?&燃气轮机;
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   pedram shabani    8 年前

    您应该检查该文件,然后,如果用户没有选择该文件,请从数据库中选择文件名

     if ($editForm->isSubmitted() && $editForm->isValid()) {
                $em = $this->getDoctrine()->getManager();
                $form->handleRequest($request);
                $TaskRepo=$em->getRepository('AppBundle:Task');
                $Taskdata = $TaskRepo->find($id);///id task
                $Taskdata->setName($form->get('name')->getData());
                $Taskdata->setDescription($form->get('description(')->getData());
                $Taskdata->setDatetime(new \DateTime('now'));
        if($form->get('brochure')->getData() != ""){////Check the file selection status
    
                 $file2 = $form->get('brochure')->getData();
                 $fileName2 = md5(uniqid()).'.'.$file2->guessExtension();
                 $file2->move(
                 $this->getParameter('brochures_directory'), $fileName2);
                 $Taskdata->setBrochure($fileName2);
            }
             $em->flush();
         } 
    
        2
  •  0
  •   Lukaszy    8 年前

    好的,我在写注释和添加任务实体代码几秒钟后发现了问题。在get宣传册方法中,我尝试为每个任务实例创建文件对象,即使它没有宣传册,所以解决方案是使用:

    public function getBrochure()
    {
        return $this->brochure;
        //return new File($this->brochure);
    }