2017-07-12 15 views
1

私は今拡張しようとしている単純なCMSであるUdemyのSymfonyチュートリアルに従っています。Symfony 3新しいファイルがアップロードされなかった場合のファイルアップロードとDB古いファイルフィールドの削除

フォームにファイルアップロードフィールドを追加しました。ファイルがアップロードされ、ファイル名がデータベースに保存されています。

編集レコードと同じように新しいレコードを追加する私は新しいファイルを選択します。編集フォームで新しいファイルを追加します。

アップロードする新しいファイルを選択せず​​に編集しようとすると、元のファイル名がdbから削除されます。

これは私が問題は私が持っていたフォームビルダdata_classから来ていると思うsymfonyのフォームビルダーは、私がこの

->add('brochure', FileType::class,[ 
      'label' => 'Image', 
      'data_class' => null, 
      'required' => false 

     ]) 

を持っている場合、私は、コントローラ

public function editAction(Request $request, Car $car) 
{ 
    $deleteForm = $this->createDeleteForm($car); 
    $editForm = $this->createForm('CarBundle\Form\CarType', $car); 


    $editForm->handleRequest($request); 


    if ($editForm->isSubmitted() && $editForm->isValid()) { 


     $file = $editForm['brochure']->getData(); 

     if(!empty($file)) { 

      // Generate a unique name for the file before saving it 
      $fileName = md5(uniqid()).'.'.$file->guessExtension(); 

      // Move the file to the directory where brochures are stored 
      $file->move($this->getParameter('brochures_directory'), $fileName); 

      $car->setBrochure($fileName); 
     } else { 

      $id = $car->getId(); 
      $em = $this->getDoctrine()->getManager(); 
      $car = $em->getRepository('CarBundle:Car')->find($id); 
      $fileName = $car->getBrochure(); 
      $car->setBrochure($fileName); 
      } 


     $em = $this->getDoctrine()->getManager(); 

     $em->merge($car); 
     $em->flush(); 
     return $this->redirectToRoute('car_edit', array('id' => $car->getId())); 

     // return $this->redirectToRoute("car_index"); 
    } 

で、これまで持っているものですエラーのために追加する

フォームの表示データは、クラス> Symfony \ Component \ HttpFoのインスタンスであると予想されますundation \ File \ Fileですが、(n)文字列です。

しかし、私はそれを修正する方法、何か提案や歓迎歓迎!

ps。私はこれがおそらくサービスであるべきであることを読んだが、赤ちゃんが最初にステップする!

+0

mayby try else blockを削除してください。 – mcek

+0

また、$ emを2回設定してリセットします。上部に$ emを設定してください – pogeybait

答えて

0

this pageの下に(ctrl + fは「既に永続化された項目を編集するためのフォームを作成しています..」)、項目を編集している場合は古いファイルの名前。

public function editAction(Request $request, Car $car) 
{ 
    $editForm = $this->createForm('CarBundle\Form\CarType', $car); 
    $editForm->handleRequest($request); 
    $brochureDir = $this->getParameter('brochures_directory'); 
    if (!empty($car->getBrochure()) { 
     $car->setBrochure(
       new File($brochureDir . '/' . $car->getBrochure() 
     ); 
    } 

    if ($editForm->isSubmitted() && $editForm->isValid()) { 
     $file = $car->getBrochure(); 
     if (!empty($file)) { 
      // Generate a unique name for the file before saving it 
      $fileName = md5(uniqid()) . '.' . $file->guessExtension(); 
      // Move the file to the directory where brochures are stored 
      $file->move($brochureDir, $fileName); 
      $car->setBrochure($fileName); 
     } 

     $em = $this->getDoctrine()->getManager(); 
     $em->flush(); 

     return $this->redirectToRoute('car_edit', array('id' => $car->getId())); 
    } 
} 

は古いエントリから新しいファイルまたはnullを今すぐあなたのフォームから「『data_class』 => NULL」を削除することができますし、それを聞かせてのいずれかです。

私はまた、あなたの2つの教義の呼び出しを取り除いて、その "マージ"を取り除いています(それは何もわかりません)。教義にはすでに存在しているので、エンティティを編集してフラッシュするだけで動作するはずです)。私はまた、いくつかの変数を設定する以外に何をしているのか分からないので、そのelseステートメントを取り除きました。このコードがうまくいかず、else文(または削除したもの)が必要な場合は、そこに戻してもう少し作業してください。これはテストされたコードではありません。

幸運。

+0

提案していただきありがとうございます。私は決して "既に永続化されたアイテムを編集するためのフォームを作成する"ことは決して考えていません。 私のクラスでは、依頼されたフォームの$ car-> getBrochure()とデータベースの$ car-> getBrochure()を区別することができません。ファイル) 他のヒントがある場合は、歓迎しますが、引き続きお試しください! –

+0

フォームのバージョンを取得するには、$ editForm ['brochure']のいずれかを実行します。または、ヌルチェックを行います。 if($ car-> getBrochure())と同様です。または両方。フォームが空の場合のように、car-> getBrochureを実行します。パンフレットが空の場合はフォームなどを入手してください – ASOlivieri

1

私は解決策を見つけたので、助けてくれたすべての人に感謝します! 他の人に見せるために私のソリューションを投稿しますが、私はSymfonyのエキスパートではないことを認識しておいてください。

public function editAction(Request $request, Car $car) 
{ 
    $deleteForm = $this->createDeleteForm($car); 
    $editForm = $this->createForm('CarBundle\Form\CarType', $car); 

    //get the current file name if there is one 
    $currentFile = $car->getBrochure(); 

    $editForm->handleRequest($request); 


    if ($editForm->isSubmitted() && $editForm->isValid()) { 

     $file = $editForm['brochure']->getData(); 

     if(!empty($file)) { 

      //if new file has been posted, use it to update DB 

      // Generate a unique name for the file before saving it 
      $fileName = md5(uniqid()).'.'.$file->guessExtension(); 

      // Move the file to the directory where brochures are stored 
      $file->move(
       $this->getParameter('brochures_directory'), 
       $fileName 
       );    
      $car->setBrochure($fileName); 

     } else { 

      //if no new file has been posted and there is a current file use that to update the DB 
      if (!empty($currentFile)) { 
       $car->setBrochure($currentFile); 

      } 

     } 


     $em = $this->getDoctrine()->getManager(); 
     $em->flush(); 
     return $this->redirectToRoute('car_edit', array('id' => $car->getId())); 

     // return $this->redirectToRoute("car_index"); 
    } 

    return array(
     'car'   => $car, 
     'edit_form' => $editForm->createView(), 
     'delete_form' => $deleteForm->createView(), 
    ); 
} 
関連する問題