没有实体的Symfony2简单文件上传编辑 [英] Symfony2 simple file upload edit without entity

查看:71
本文介绍了没有实体的Symfony2简单文件上传编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请在这里帮助我,因为我无法相信自己的眼睛。

Please help me out here because I can't belive my eyes.

我拒绝使用某些第三方插件进行文件上传,并且拒绝为文件/文档创建单独的实体。我只想在Zend / Laravel中进行简单的文件上传,依此类推。

I refuse to use some 3rd party plugin for file uploads, and refuse to creat a separate entity for a file/document. I just want simple file upload that I would do in Zend/Laravel and so on.

我有一个发票表,最后一个列名为 attachment,我想在此处存储其经过消毒的名称(例如:123421_filename.jpg),添加表单和上载都很好。

I have a invoice table with the last collumb name "attachment", I want to store here its sanitized name (eg: 123421_filename.jpg ), the addition form and upload went well.

此处的代码:

//AddAction
     $file=$form['attachment']->getData();
        $fileName=$file->getClientOriginalName();
        $sanitizedFilename=rand(1, 99999).'_'.$fileName;
        $dir='files/'.$userId.'/';
        $file->move($dir, $sanitizedFilename);
    ...
        $invoice->setAttachment($sanitizedFilename);
        $em = $this->getDoctrine()->getManager();
        $em->persist($invoice);

我遇到的第一个问题是不知道如何解决的,就是使用编辑表单。表单生成器

First problem I have and don't know how to solve is using the edit form.. I have a formbuilder

   $builder->add('attachment', 'file',array('data_class'=>null))

因为我的文件没有对象,而且在我的发票表中我将名称存储为字符串,这里出现错误,我需要强制data_class => null ..这很糟糕,因为如果我在前端为上载字段编辑发票,则会得到NULL而不是文件名链接到发票的当前文件。

Because I don't have a "Object" for my file and because in my invoice table I store the name as a string, I get an error here and I need to force the data_class => null .. which is bad, because if I edit a Invoice in the frontend for the upload field I get NULL, instead of the filename of the current file linked to the invoice.

使用:

$builder->add('attachment', 'text')

我只是一次愚蠢的文本框而不会得到文件输入-上面有名字-..那我该如何解决呢?

I don't get the file input just a silly textbox BUT THIS TIME --with the name in it-- .. so how do I solve this? File Input widget and file name in it without a Document object ?!

还有第二个问题(我只是扔掉我到目前为止开发的所有应用程序,搬到laravel,因为我遇到了很多这类问题 ..在symfony中做简单的事情几乎总是比在其他框架中复杂..或者也许我的错是我不想遵循指南并创建一个文档实体?!如果我想要灵活性并且想以不适合所有这些指南的另一种方式来管理我的数据库怎么办?)

And the second question ( I am merely at the point of throwing out all my application developed until now and move to laravel because I had a lot of "these kind of problems" ..doing simple things in symfony is almost always more complicated than in other frameworks..or maybe its my fault that I don't want to folow the guidelines and create a Document Entity?! What if I want flexibility and want to manage my database in another manner that doesn't fit in all these guidelines?)

所以我在编辑表单操作,我上传了一个新文件(不要忘记将文件设置为$ builder-> add('attachment',file',array('data_class'=> null))

So I am on the edit form action, I upload a new file (don't forget that I have the file set to $builder->add('attachment',file',array('data_class'=>null))

我无法从正在编辑的当前发票中获取附件字符串名称吗?!

I can't get my attachment string name from the current invoice I am editing? !

public function editAction($id)
   ....
   $invoice = $em->getRepository('AppBundle:Invoice')->find($id);
   ..
    if ($form->isValid()) {

            $oldFileName=$invoice->getAttachment();  //this is null
            $oldFileName=$invoice->getId();   //this returns the invoice id
            $oldFileName=$invoice->getValue(); //returns invoice value

                echo 'old: '.$oldFileName.'<br/>';
                exit();
     }

所以有人请告诉我为什么我无法访问我的发票资源?

So someone please tell me why I can't access my invoices property? Which is a string ?

我尝试创建一个新实例,但是如果我用$ invoice对象创建表单,它将以某种方式将他链接到编辑的附件形式

I tried making a new instance I though that somehow if I create the form with the $invoice object it somehow links him to the attachment from the edit form

 if ($form->isValid()) {
    $sameInvoice= $em->getRepository('AppBundle:Invoice')->find(20); //hardcoded ID

                   $oldFileName=$sameInvoice->getAttachment();   //still null
                   $oldFileName=$sameInvoice->getId();   //returns 20
                    echo 'old: '.$oldFileName.'<br/>';
                    exit();

我唯一想要的是在发票表中有一个文件名字符串,并测试自己是否文件存在于具有该文件名的路径中,如果存在,则将其删除并上传新文件名,依此类推。.为什么它必须这么难???

The only thing I wanted was, have a filename string in my invoice table, and test myself if the file exists in the path with that filename and if it exists, then delete it and upload the new one and so on.. why does it have to be so hard ??

为什么我必须创建一个实体?为什么我必须更改数据库结构(这里不是这种情况。但是,如果客户端不希望更改数据库该怎么办..因此我无法插入此文档表)。表单生成器显示了发票->附件中的数据,我明白这是因为他需要文件数据并且他不能接受字符串,但是为什么没有这些简单任务的准则?

Why do I have to create a entity ? Why do I have to alter my database structure (its not the case here..but what if the client doesn't want the database to be changed..so I can't insert this "Document" table).. Why can't the form builder show the data from invoice->attachment, I get it that because he needs a file data and he can't accept a string, but why aren't there guidelines for these simple tasks?!

推荐答案

好了,我终于设法使其工作了:

Alright I finally managed to get it working:

所以这是为什么NULL是因为它确实将$ invoice实例绑定到以下形式:

So the problem why it was NULL was because it did bind the $invoice instance to the form:

$invoice = $em->getRepository('AppBundle:Invoice')->find($id);
$form = $this->createForm(new InvoiceType($id), $invoice);

在这段代码之后,所有引用如:$ invoice-> getAttachment();

After this block of code, all refferences like : $invoice->getAttachment();

被引用为表单发票对象而不是实际对象,因此基本上在此之后,我只能调用诸如 getClientOriginalName()之类的东西,并向我显示新文件

Are reffered to as the form invoice object not the actual object, so basically after this I can only call things like: "getClientOriginalName()" and would show me the new file that I uploaded in the edit form.

为了解决这个问题,我创建了一个变量,该变量最初在$ invoice对象之前存储该名称,以绑定到表单

To counter this I created a variable that stores that name initially before the $invoice object to get bind to the form

    $invoice = $em->getRepository('AppBundle:Invoice')->find($id);
    $oldFileName=$invoice->getAttachment();  //this is stil the object here I can see the record from the database
    $form = $this->createForm(new InvoiceType($id), $invoice);
  //here the $invoice->getAttachment(); returns null

所以现在我有了旧名称,可以像这样测试它了:

So now I have the old name and I can test it like:

 if ($form->isValid()) {

                $fs=new Filesystem();
                $newFile=$form['attachment']->getData();
                $newFileName=$newFile->getClientOriginalName();

                if($newFileName != $oldFileName)
                {
                  //  if($fs->exists('files/'.$this->getUser()->getId().'/'.$oldFileName))
                  //      $fs->remove('files/'.$this->getUser()->getId().'/'.$oldFileName);

                    $sanitizedFilename=rand(1, 99999).'_'.$newFileName;
                    $dir='files/'.$this->getUser()->getId().'/';
                    $newFile->move($dir, $sanitizedFilename);
                    $invoice->setAttachment($sanitizedFilename);
                }

                $em->persist($invoice);
                $em->flush();

                return $this->redirect($this->generateUrl('my-invoices'));
            }

另一个问题是旧文件名没有出现在编辑中发票页面我将变量发送到视图:

For the other problem with the filename of the old file not appearing in the Edit Invoice Page I sent my variable to the view :

return $this->render('AppBundle:Invoices:edit.html.twig', array(
            'oldFileName' => $oldFileName) 

那个值用小枝打到输入文件小部件的标签上

And put that value to the label of the input file widget using twig

这篇关于没有实体的Symfony2简单文件上传编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆