CollectionType实体问题

时间:2015-12-07 13:14:11

标签: php forms symfony doctrine entity

我正在尝试创建一个可以处理多个文件上传的表单。到现在为止还挺好。我有两个表图像和产品,它们具有ManyToOne关系。我正在为图像使用CollecitonType字段,因此我可以动态地动态添加/删除字段。问题是我在表单呈现

上遇到此异常
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 个答案:

答案 0 :(得分:1)

首先: 在Product class

您的变量名为private $image,应为private $images;用's'代表多毛。

第二次

在您的“产品表单”构建器中,您要求提供名为ImageFile的字段,但该字段在Product class中不存在(但存在于Image class上),它应为{{1} }}