2017-02-15 43 views
2

我需要使用附件編輯表單。我在DB中保存了一個附件的路徑。 將路徑保存到表數據庫中的文件保存後。我如何編輯帶附件的表單[Symfony]

我的項目是這裏https://github.com/zchipirov/delivery

我的表格:

->add('photo', FileType::class) 

控制器動作:

public function newAction(Request $request) { 
     $em = $this->getDoctrine()->getManager(); 
     $repo = $em->getRepository('DeliveryAdminBundle:MyEntity'); 
     $_arr = $request->request->get('my_entity'); 

     $entity = new MyEntity(); 
     $form = $this->createForm(MyEntityType::class, $entity, [ 
      'entity_manager' => $this->get('doctrine.orm.entity_manager') 
     ]); 

     $kitchen_array = $_arr['kitchen']; 
     $specialization_array = $_arr['specialization']; 

     $form->handleRequest($request); 

     if ($form->isSubmitted() && $form->isValid()) { 
      $dir = './entity/'.Additional::get_in_translate_to_en($entity->getTitle()); 
      if (!file_exists($dir)) { 
       mkdir ($dir); 
      } 
      // $dir .= '/'.Additional::get_in_translate_to_en($entity->getTitle()).'.jpg'; 
      $file_name = Additional::get_in_translate_to_en($entity->getTitle()).'.jpg'; 
      $form['photo']->getData()->move($dir.'/', $file_name); 

      $identity = $repo->save($entity, $dir.'/'.$file_name); 
      $repo_spec = $em->getRepository('DeliveryAdminBundle:MyEntitySpecialization'); 
      $repo_spec->add($identity, $specialization_array); 

      $repo_kitchen = $em->getRepository('DeliveryAdminBundle:MyEntityKitchen'); 
      $repo_kitchen->add($identity, $kitchen_array); 

      return $this->redirectToRoute('show_myentity'); 
     } 

     return $this->render('DeliveryAdminBundle:Entity:new.html.twig', 
       array('form'=>$form->createView())); 
    } 

HTML:

<div class="form-group"> 
         <label> 
          {{ form_label(form.photo, 'Логотип заведения') }} 
         </label> 
          {{ form_errors(form.photo) }} 
          <span class="btn btn-success fileinput-button"> <i class="glyphicon glyphicon-plus"></i> 
           <span>Добавить файл...</span> 
           {{ form_widget(form.photo, { 'attr': {'class': 'form-control'} }) }} 
          </span>       
          <br/> 
          <br/> 
          {{ form_widget(form.save, { 'attr': {'class': 'btn btn-primary btn-sm'} }) }} 
          <button class="btn btn-wide btn-sm" onclick="location='{{ path('show_kitchen') }}';">Отмена</button> 
        </div> 
+0

@Rooneyl在Symfony 2和symfony3之間在這方面有什麼不同嗎?如果是這樣,並且重複的原始文件仍然有效,那麼它的標題應該重新編寫以刪除版本2特定的文字。 – Martin

回答

0

您必須將「empty_data」與文件路徑添加到FileType以設置默認數據。這個解決方案沒有檢查,但我認爲它應該可以工作

+0

錯誤:表單的視圖數據預計是類Symfony \ Component \ HttpFoundation \ File \ File的一個實例,但是是一個(n)字符串。您可以通過將「data_class」選項設置爲null或通過添加將(n)字符串轉換爲Symfony \ Component \ HttpFoundation \ File \ File實例的視圖轉換器來避免此錯誤。 – Zaur

+0

Витайлий,Вынавернякапонимаетепорусски)Яхочусохранитьнесамфайлвбинарномидевбазе,апутькнему。可以使用一個或多個軟件,並且可以使用其他軟件來實現。 – Zaur

+0

使用Symfony \ Component \ HttpFoundation \ File \ File;使用Symfony \ Component \ HttpFoundation \ File \ File; 新文件($ pathToFile); 'data'中的數據轉換器 將數據轉換爲數據 –

相關問題