Symfony:如何上传多个文件

时间:2017-02-04 21:37:46

标签: php symfony

我对Symfony很陌生,我尝试使用Symfony Forms和Vichuploader-Bundle上传多个文件(图像)。

我的TestObjectType看起来像这样,它应该包含Image对象的集合:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('images', CollectionType::class, array(
            'entry_type' => new ImageFormType(),
        ));
}
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => TestObject::class
    ));
}

ImageFormType:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('imageFile', VichImageType::class, array(
                'required' => false,
            ));
    }
   public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => Image::class
        ));
    }

部分控制器代码:

public function newAction(Request $request)
        $form = $this->createForm(TestObjectType::class, $testObject);
        $form->handleRequest($request);

        if ($form->isValid()) {  
            $em = $this->getDoctrine()->getManager();
            $em->persist($testObject);
            $em->flush();

            return $this->redirectToRoute("home");
        }
}

提交时出现以下错误: Neither the property "imageFile" nor one of the methods "getImageFile()", "imageFile()", "isImageFile()", "hasImageFile()", "__get()" exist and have public access in class "TestBundle\Entity\TestObject".

我的TestObject没有imageFile属性,但是我的Image对象没有。那么这里缺少什么?为什么不使用Image对象的imageFile属性?我已经阅读了How to Upload FilesCollectionType Field,但它没有多大帮助。

编辑

也许我的实体片段可能有用:

的TestObject

/**
 * @ORM\OneToMany(targetEntity="TestBundle\Entity\Image", mappedBy="testObject")
 */
private $images;

图像

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

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


    /**
     * @ORM\ManyToOne(targetEntity="TestBundle\Entity\TestObject", inversedBy="images")
     * @ORM\JoinColumn(name="test_object_id", referencedColumnName="id")
     */
    private $testObject;

1 个答案:

答案 0 :(得分:0)

我想我找到了解决方案:Symfony的缓存让我生气!一个简单的

php app/console cache:clear

做了伎俩!唉,这花了我几个小时!