2017-10-17 11 views
0

이미지를 업로드하여 기사를 게시 할 수있는 양식이 관리 인터페이스에 있고 양식을 편집 할 수있는 다른 양식이 있습니다.필드가 비어 있으면 이미지를 업데이트하고 이전 이미지를 그대로 유지하십시오.

내 기사를 수정할 때 필드가 비어 있거나 ("null") 이전 이미지를 유지하고 싶다면 이전 이미지를 삭제하여 새 이미지를 인스턴스화하고 싶지만 성공하지 못합니다. .

현재 내 기사를 수정하여 필드 이미지를 비워두면 doctrine이 내 필드 image_id를 데이터베이스에서 NULL로 정의합니다.

엔티티 "기사"및 현재 항목의 데이터를 기본값으로 설정하는 EditArticleType을 사용하여 OneToOne과 관련된 "이미지"개체가 있습니다.

내 컨트롤러 : 당신의 도움에 미리

public function modifierArticleAction(Article $article, Request $request, $id) 
    { 
     $em = $this->getDoctrine()->getManager(); 
     $form = $this->createForm(EditArticleType::class, $article); 
     $form->handleRequest($request); 

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

      // $file récupère l'image 
      $file = $article->getImage(); 
      $file2 = $article->getImage2(); 
      $file3 = $article->getImage3(); 
      $file4 = $article->getImage4(); 
      $file5 = $article->getImage5(); 
      $file6 = $article->getImage6(); 
      $file7 = $article->getImage7(); 
      $file8 = $article->getImage8(); 
      $file9 = $article->getImage9(); 
      $file10 = $article->getImage10(); 

      if($file !== null && ($file instanceof UploadedFile) === true) { 
       $fileName = md5(uniqid('img_', true)); 
       $fileExtension = $file->guessExtension(); 
       $nomImageCompletFile = $fileName . '.' . $fileExtension; 

       $file->move(
        $this->getParameter('articles_images_directory'), 
        $nomImageCompletFile 
       ); 

       setKey('xxx'); 
       $tinified = fromFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile); 
       $tinified->toFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile); 

       $image = new Image(); 
       $image->setExtension($fileExtension); 
       $image->setFile($fileName); 
       $em->persist($image); 
       $article->setImage($image); 
      } 

      if($file2 !== null && ($file2 instanceof UploadedFile) === true) { 
       $file2Name = md5(uniqid('img_', true)); 
       $file2Extension = $file2->guessExtension(); 
       $nomImageCompletFile2 = $file2Name . '.' . $file2Extension; 

       $file2->move(
        $this->getParameter('articles_images_directory'), 
        $nomImageCompletFile2 
       ); 

       setKey('xxx'); 
       $tinified = fromFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile2); 
       $tinified->toFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile2); 

       $image2 = new Image(); 
       $image2->setExtension($file2Extension); 
       $image2->setFile($file2Name); 
       $em->persist($image2); 
       $article->setImage2($image2); 
      } 

      if($file3 !== null && ($file3 instanceof UploadedFile) === true) { 
       $file3Name = md5(uniqid('img_', true)); 
       $file3Extension = $file3->guessExtension(); 
       $nomImageCompletFile3 = $file3Name . '.' . $file3Extension; 

       $file3->move(
        $this->getParameter('articles_images_directory'), 
        $nomImageCompletFile3 
       ); 

       setKey('xxx'); 
       $tinified = fromFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile3); 
       $tinified->toFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile3); 

       $image3 = new Image(); 
       $image3->setExtension($file3Extension); 
       $image3->setFile($file3Name); 
       $em->persist($image3); 
       $article->setImage3($image3); 
      } 

      if($file4 !== null && ($file4 instanceof UploadedFile) === true) { 
       $file4Name = md5(uniqid('img_', true)); 
       $file4Extension = $file4->guessExtension(); 
       $nomImageCompletFile4 = $file4Name . '.' . $file4Extension; 

       $file4->move(
        $this->getParameter('articles_images_directory'), 
        $nomImageCompletFile4 
       ); 

       setKey('xxx'); 
       $tinified = fromFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile4); 
       $tinified->toFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile4); 

       $image4 = new Image(); 
       $image4->setExtension($file4Extension); 
       $image4->setFile($file4Name); 
       $em->persist($image4); 
       $article->setImage4($image4); 
      } 

      if($file5 !== null && ($file5 instanceof UploadedFile) === true) { 
       $file5Name = md5(uniqid('img_', true)); 
       $file5Extension = $file5->guessExtension(); 
       $nomImageCompletFile5 = $file5Name . '.' . $file5Extension; 

       $file5->move(
        $this->getParameter('articles_images_directory'), 
        $nomImageCompletFile5 
       ); 

       setKey('xxx'); 
       $tinified = fromFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile5); 
       $tinified->toFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile5); 

       $image5 = new Image(); 
       $image5->setExtension($file5Extension); 
       $image5->setFile($file5Name); 
       $em->persist($image5); 
       $article->setImage5($image5); 
      } 

      if($file6 !== null && ($file6 instanceof UploadedFile) === true) { 
       $file6Name = md5(uniqid('img_', true)); 
       $file6Extension = $file6->guessExtension(); 
       $nomImageCompletFile6 = $file6Name . '.' . $file6Extension; 

       $file6->move(
        $this->getParameter('articles_images_directory'), 
        $nomImageCompletFile6 
       ); 

       setKey('xxx'); 
       $tinified = fromFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile6); 
       $tinified->toFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile6); 

       $image6 = new Image(); 
       $image6->setExtension($file6Extension); 
       $image6->setFile($file6Name); 
       $em->persist($image6); 
       $article->setImage6($image6); 
      } 

      if($file7 !== null && ($file7 instanceof UploadedFile) === true) { 
       $file7Name = md5(uniqid('img_', true)); 
       $file7Extension = $file7->guessExtension(); 
       $nomImageCompletFile7 = $file7Name . '.' . $file7Extension; 

       $file7->move(
        $this->getParameter('articles_images_directory'), 
        $nomImageCompletFile7 
       ); 

       setKey('xxx'); 
       $tinified = fromFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile7); 
       $tinified->toFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile7); 

       $image7 = new Image(); 
       $image7->setExtension($file7Extension); 
       $image7->setFile($file7Name); 
       $em->persist($image7); 
       $article->setImage7($image7); 
      } 

      if($file8 !== null && ($file8 instanceof UploadedFile) === true) { 
       $file8Name = md5(uniqid('img_', true)); 
       $file8Extension = $file8->guessExtension(); 
       $nomImageCompletFile8 = $file8Name . '.' . $file8Extension; 

       $file8->move(
        $this->getParameter('articles_images_directory'), 
        $nomImageCompletFile8 
       ); 

       setKey('xxx'); 
       $tinified = fromFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile8); 
       $tinified->toFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile8); 

       $image8 = new Image(); 
       $image8->setExtension($file8Extension); 
       $image8->setFile($file8Name); 
       $em->persist($image8); 
       $article->setImage8($image8); 
      } 

      if($file9 !== null && ($file9 instanceof UploadedFile) === true) { 
       $file9Name = md5(uniqid('img_', true)); 
       $file9Extension = $file9->guessExtension(); 
       $nomImageCompletFile9 = $file9Name . '.' . $file9Extension; 

       $file9->move(
        $this->getParameter('articles_images_directory'), 
        $nomImageCompletFile9 
       ); 

       setKey('xxx'); 
       $tinified = fromFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile9); 
       $tinified->toFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile9); 

       $image9 = new Image(); 
       $image9->setExtension($file9Extension); 
       $image9->setFile($file9Name); 
       $em->persist($image9); 
       $article->setImage9($image9); 
      } 

      if($file10 !== null && ($file10 instanceof UploadedFile) === true) { 
       $file10Name = md5(uniqid('img_', true)); 
       $file10Extension = $file10->guessExtension(); 
       $nomImageCompletFile10 = $file10Name . '.' . $file10Extension; 

       $file10->move(
        $this->getParameter('articles_images_directory'), 
        $nomImageCompletFile10 
       ); 

       setKey('xxx'); 
       $tinified = fromFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile10); 
       $tinified->toFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile10); 

       $image10 = new Image(); 
       $image10->setExtension($file10Extension); 
       $image10->setFile($file10Name); 
       $em->persist($image10); 
       $article->setImage10($image10); 
      } 

      $slugify = new Slugify(); 
      $article->setTitre($article->getTitre()); 
      $article->setIntroduction($article->getIntroduction()); 
      $article->setContenu($article->getContenu()); 
      $article->setSlug($slugify->slugify($article->getTitre())); 

      $em->flush(); 

      $this->addFlash('success', 'Votre article a bien été modifié :)'); 

      return $this->redirectToRoute('admin_sg_modifier_article', ['id' => $id]); 
     } 

     return $this->render(
      'AdminSGBundle:page:modifier-article.html.twig', [ 
                   'form'   => $form->createView(), 
                   'articleToUpdate' => $article, 
                  ] 
     ); 
    } 

감사

답변

0

당신 기사 개체에서 다음과 같이 각각의 이미지 필드를 편집하려고 :

public function setImage($image) 
{ 
    if (!is_null($image)) { 
     $this->image = $image; 
    } 

    return $this; 
} 

을 ...

public function setImage10($image) 
{ 
    if (!is_null($image)) { 
     $this->image10 = $image; 
    } 

    return $this; 
} 

이 논리는 null 인 경우 이미지 엔티티를 설정하지 못하게해야합니다.