2015-12-07 41 views
2

那麼我想創建一個可以處理多個文件上傳的表單。到現在爲止還挺好。我有兩個表格圖像和產品有ManyToOne關係。我爲圖像使用了CollecitonType字段,所以我可以動態地添加/刪除字段。問題是我得到這種形式渲染異常CollectionType實體問題

Neither the property "imageFile" nor one of the methods "getImageFile()", "imageFile()", "isImageFile()", "hasImageFile()", "__get()" exist and have public access in class "AppBundle\Entity\Product". 

我在做什麼錯了?

的appbundle \實體\圖片

<?php 

namespace AppBundle\Entity; 

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


/** 
* Product 
* 
* @ORM\Table() 
* @ORM\Entity 
*/ 
class Image 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 


    /** 
    * @ORM\ManyToOne(targetEntity="Product") 
    */ 
    protected $product; 


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

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

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



    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * 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 
    */ 
    public function setImageFile(File $image = null) 
    { 
     $this->imageFile = $image; 

     if ($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 \DateTime('now'); 
     } 
    } 

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

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

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

    /** 
    * Set updatedAt 
    * 
    * @param \DateTime $updatedAt 
    * 
    * @return Image 
    */ 
    public function setUpdatedAt($updatedAt) 
    { 
     $this->updatedAt = $updatedAt; 

     return $this; 
    } 

    /** 
    * Get updatedAt 
    * 
    * @return \DateTime 
    */ 
    public function getUpdatedAt() 
    { 
     return $this->updatedAt; 
    } 

    /** 
    * Set product 
    * 
    * @param \AppBundle\Entity\Product $product 
    * 
    * @return Image 
    */ 
    public function setProduct(\AppBundle\Entity\Product $product = null) 
    { 
     $this->product = $product; 

     return $this; 
    } 

    /** 
    * Get product 
    * 
    * @return \AppBundle\Entity\Product 
    */ 
    public function getProduct() 
    { 
     return $this->product; 
    } 
} 

的appbundle \實體\產品

<?php 

namespace AppBundle\Entity; 
use Doctrine\Common\Collections\ArrayCollection ; 


use Doctrine\ORM\Mapping as ORM; 

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

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



    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set name 
    * 
    * @param string $name 
    * 
    * @return Product 
    */ 



    private $image; 

    /** 
    * Constructor 
    */ 

    public function __construct() 
    { 
     $this->image = new ArrayCollection; 
    } 



    public function setName($name) 
    { 
     $this->name = $name; 

     return $this; 
    } 

    /** 
    * Get name 
    * 
    * @return string 
    */ 
    public function getName() 
    { 
     return $this->name; 
    } 

    public function getImages() 
    { 
     return $this->images; 
    } 

    public function addImage(ImageInterface $image) 
    { 
     if (!$this->images->contains($image)) { 
      $this->images->add($image); 
     } 

     return $this; 
    } 

    public function removeImage(ImageInterface $image) 
    { 
     $this->images->remove($image); 

     return $this; 
    } 

    public function setImages(Collection $images) 
    { 
     $this->images = $images; 
    } 
} 

的appbundle \表格\將ImageType

.............. 



    public function buildForm(FormBuilderInterface $builder, array $options) 
     { 
      $builder 
       ->add('imageFile', 'file') 
      ; 
     } 


     /** 
     * @param OptionsResolverInterface $resolver 
     */ 
     public function configureOptions(OptionsResolver $resolver) 
     { 
      $resolver->setDefaults(array(
       'data_class' => 'AppBundle\Entity\Image', 
       'attr'=> array('novalidate'=>'novalidate') 
      )); 
     } 
     ............. 

的appbundle \表格\ ProductType

........... 
     public function buildForm(FormBuilderInterface $builder, array $options) 
     { 
      $builder 
       ->add('name', 'text') 
       ->add('imageFile', 'collection', array(
        'allow_add' => true, 
        'allow_delete' => true, 
        'required'  => false, 
        'type'   => new ImageType(), 
        'prototype' => true, 
        'attr' => array(
         'class' => 'selection', 
        ), 
       )) 
       ->add('upload', 'submit') 
      ; 
     } 

     public function configureOptions(OptionsResolver $resolver) 
     { 
      $resolver->setDefaults(array(
       'data_class' => 'AppBundle\Entity\Product', 
       'attr'=> array('novalidate'=>'novalidate') 
      )); 
     } 
    ...... 

回答

1

首先: 在你Product class

你的變量命名爲private $image,它應該是private $images;與'S'爲多。

在你的產品表單生成器,你問一個名爲ImageFile領域,但它不存在Product class(但存在於Image class),它應該是images