2017-09-13 1 views
1

나는 이것에 대해 잠시 책을 읽었지만 나에게 맞는 것은 아무것도 발견하지 못했습니다. 이 엔티티편집시 Symfony 파일 필드가 비어 있음

/** 
* @ORM\Column(type="string") 
* 
* @Assert\NotBlank(message="Molimo unesite PDF ili Word fajl.") 
* @Assert\File(mimeTypes={ "application/vnd.openxmlformats-officedocument.presentationml.presentation", "application/vnd.ms-powerpoint", "application/pdf", "application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"}) 
*/ 
private $body; 

인이

// Problem with empty field on edit persist! Form expect FileType but db sends string!!! 
      ->add('body', FileType::class, [ 
       //'data_class' => null,// <-- If I change it nothing happened. 
       'label' => 'Word ili pdf dokument', 
       'invalid_message' =>'Id posta nije validan', 
       'attr' => [ 
        'class' => 'form-group', 
     ]]) 

모든 것이 매우 간단하며 다음과 같은 문서를 만든 형태

입니다. 나는 파일을위한 몇 가지 속성 중 하나 ($ body)를 포함하는 엔티티가 있습니다. 파일은 web/uploads/post /에 저장되도록 설정됩니다. 페이지를 편집 할 때 다음 오류가 표시됩니다.

"양식의보기 데이터는 Symfony \ Component \ HttpFoundation \ File \ File 클래스의 인스턴스가 될 것으로 예상되지만 (n) 문자열입니다. 이것은 "data_class"옵션을 null로 설정하거나 문자열을 Symfony \ Component \ HttpFoundation \ File \ File의 인스턴스로 변환하는 뷰 변환기를 추가함으로써 가능합니다. "

내가 data_status => null 필드를 설정하면 비어 있습니다.

내가 필요한 것은이 작업에 대한 예제 링크 일 것입니다. 아마도 데이터 변압기입니까?

+0

사실 어떤 제안이라도 환영합니다! –

+0

직면하고있는 문제에 대한 정보를 조금 더 제공해 주시겠습니까? – Toastrackenigma

+0

약간의 수정을했습니다. 필요한 정보가 있으면 나는 쓸 준비가되어있다. –

답변

0

editAction에 새 File()을 만들어이 문제를 해결할 수 있습니다. 좋아요 :

/** 
* @Route("/edit/{yourEntity}", name="entity_edit") 
* 
* @return Response 
*/ 
public function editAction(Request $request, YourEntity yourEntity) 
{ 
    $body = new File($this->getParameter('your_files_directory') . '/' . $yourEntity->getBody()); 
    $yourEntity->setBody($body); 
    // rest of your editAction 
} 
+0

감사! 내가 코드를 구현 한 후. 나는 여전히 파일 필드가 비어 있지만 양식에서 해당 필드를 제거한 후에는 편집에 대한 예외가 없습니다. 좋지 않아, 알아,하지만 정말 파일 이름을 바꿀 필요가있어. 다시 한 번 감사드립니다! –

0

좋아, 해결되었습니다. @ASOlivieri의 대답은 하나의 작은 수정으로 훌륭합니다. 답안의 코드는 파일 이름이있는 경로를 기억합니다. 따라서 다시 편집하려면 예외가 발견되지 않을 것입니다. 원래의 파일 이름과 동일하도록 setBody를 다시 설정해야합니다. flush() 전에 설정하십시오.

public function editAction(Request $request, Post $post) { 
    $fileName = $post->getBody(); 
    $file = new File($this->getParameter('post_directory') . '/' . $post->getBody()); 
    $post->setBody($file); 

    $deleteForm = $this->createDeleteForm($post); 
    $editForm = $this->createForm('AppBundle\Form\PostEditType', $post); 
    $editForm->handleRequest($request); 


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

     $post->setBody($fileName); 
     $this->getDoctrine()->getManager()->flush(); 

희망이 있습니다.

+0

당신이 그것을 알아 낸 것을 기쁘게 생각합니다! – ASOlivieri

관련 문제