2017-06-06 10 views
0

私はVichUploaderBundleに問題がありますが、すべての手順に従っていますが、画像がアップロードできず、データベースが書き込まれなかったため、コントローラが仕上がりフォームの後にどのように処理するべきかわかりません。VichUploaderBundleを使用して画像をアップロード

私のエンティティクラス:

<?php 
namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Symfony\Component\HttpFoundation\File\File; 
use Vich\UploaderBundle\Mapping\Annotation as Vich; 
use Symfony\Component\HttpFoundation\File\UploadedFile; 


/** 
* @ORM\Entity 
* @Vich\Uploadable 
*/ 
class Product 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    // ..... other fields 

    /** 
    * NOTE: This is not a mapped field of entity metadata, just a simple property. 
    * 
    * @Vich\UploadableField(mapping="product_image", fileNameProperty="imageName", size="imageSize") 
    * 
    * @var File 
    */ 
    private $imageFile; 

    /** 
    * @ORM\Column(type="string", length=255) 
    * 
    * @var string 
    */ 
    private $imageName; 

    /** 
    * @ORM\Column(type="integer") 
    * 
    * @var integer 
    */ 
    private $imageSize; 

    /** 
    * @ORM\Column(type="datetime") 
    * 
    * @var \DateTime 
    */ 
    private $updatedAt; 

    /** 
    * If manually uploading a file (i.e. not using Symfony Form) ensure an instance 
    * of 'UploadedFile' is injected into this setter to trigger the update. If this 
    * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter 
    * must be able to accept an instance of 'File' as the bundle will inject one here 
    * during Doctrine hydration. 
    * 
    * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image 
    * 
    * @return Product 
    */ 
    public function setImageFile(File $image = null) 
    { 
     $this->imageFile = $image; 

     if ($image) { 
      var_dump($image); 
      // It is required that at least one field changes if you are using doctrine 
      // otherwise the event listeners won't be called and the file is lost 
      $this->updatedAt = new \DateTimeImmutable(); 
     } 

     return $this; 
    } 

    /** 
    * @return File|null 
    */ 
    public function getImageFile() 
    { 
     return $this->imageFile; 
    } 

    /** 
    * @param string $imageName 
    * 
    * @return Product 
    */ 
    public function setImageName($imageName) 
    { 
     $this->imageName = $imageName; 

     return $this; 
    } 

    /** 
    * @return string|null 
    */ 
    public function getImageName() 
    { 
     return $this->imageName; 
    } 

    /** 
    * @param integer $imageSize 
    * 
    * @return Product 
    */ 
    public function setImageSize($imageSize) 
    { 
     $this->imagesize = $imageSize; 

     return $this; 
    } 

    /** 
    * @return integer|null 
    */ 
    public function getImageSize() 
    { 
     return $this->imageSize; 
    } 
} 

私にformTypeクラス:

<?php 
namespace AppBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolver; 
use Symfony\Component\Form\Extension\Core\Type\FileType; 
class AvatarType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 


      ->add('imageFile', FileType::class, array('label' => 'Brochure (PDF file)')) 

     ; 
    } 

    public function getName() 
    { 
     return 'avatar_form'; 
    } 
} 

設定:

vich_uploader: 
    db_driver: orm # or mongodb or propel or phpcr 
    mappings: 
     product_image: 
      uri_prefix:   /image/avatar 
      upload_destination: '%kernel.root_dir%/../web/image/avatar' 

小枝テンプレート:

<div> 
     {{ form_start(form) }} 
    {{ form_label(form) }} 
    {{ form_errors(form) }} 
    {{ form_widget(form.imageFile) }} 
     <button type="submit" class="btn btn-default">Submit</button> 
     {{ form_end(form) }} 
</div> 

、最終的にはコントローラ:すべてのあなたのアドバイスのための

namespace AppBundle\Controller\User; 
use AppBundle\Entity\Product; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 
use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\Response; 
use Symfony\Component\HttpFoundation\Session\Session; 
use AppBundle\Controller\InitController; 
use AppBundle\Form\AvatarType; 

class UserController extends Controller implements InitController 
{ 
    /** 
    * @Route("/user/avatar", name="AvatarAction") 
    */ 
    public function AvatarAction(Request $request) 
    { 

     $form = $this->createForm('AppBundle\Form\AvatarType',null,array(
      // To set the action use $this->generateUrl('route_identifier') 
      'action' => $this->generateUrl('AvatarAction'), 
      'method' => 'POST' 
     )); 

     $form->handleRequest($request); 
     if ($request->isMethod('POST')) { 

      if($form->isValid()){ 

       //$track = new Product(); 
       //$em = $this->getDoctrine()->getManager(); 
       //$em->persist($track); 
       //$em->flush(); 
      } 

     return $this->render('default/avatar.html.twig',array(
      'form' => $form->createView() 
     )); 
    } 
} 

ありがとう!

答えて

1

多分これが解決策ではありませんが、あなたのライン:

->add('imageFile', FileType::class, array('label' => 'Brochure (PDF file)')): 

は次のようになります。

->add('imageFile', VichFileType::class, array('label' => 'Brochure (PDF file)')) 

詳細情報:symfonyはエラーとしてあなたに伝え何https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/form/vich_file_type.md

おはようです。

+0

ようこそ。 Stackoverflowはフォーラムではない、それの構造を維持してください。これは答えよりもコメントです。十分な評判があれば、コメントを投稿できます。 –

関連する問題