Symfony2 simple file upload edit without entity

人走茶凉 提交于 2019-12-05 12:08:20

Alright I finally managed to get it working:

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

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

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.

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

Remain calm. You are making this much harder then it needs to be.

myControllerAction()
{
    // No need for an object, an array works fine
    $model = array(
        'invoice' => $invoice,
        'attachment' => null
    );

    $builder = $this->createFormBuilder($model);

    $builder->setAction($this->generateUrl('cerad_game_schedule_import'));
    $builder->setMethod('POST');

    $builder->add('attachment', 'file');
    $builder->add('invoice', new InvoiceType());

    $builder->add('import', 'submit', array(
        'label' => 'Import From File',
        'attr' => array('class' => 'import'),
    ));        
    $form = $builder->getForm();

    $form->handleRequest($request);

    if ($form->isValid())
    {   
        $model = $form->getData();
        $file = $model['attachment']; // The file object is built by the form processor

        if (!$file->isValid())
        {
            $model['results'] = sprintf("Max file size %d %d Valid: %d, Error: %d<br />\n",
            $file->getMaxFilesize(), // Returns null?
            $file->getClientSize(),
            $file->isValid(), 
            $file->getError());
            die($model['results'];
        }
        $importFilePath = $file->getPathname();
        $clientFileName = $file->getClientOriginalName();

        // Xfer info to the invoice object and update

    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!