在 Symfony2 中使用 VlabsMediaBundle 上传多个文件

Multiple file upload with VlabsMediaBundle in Symfony2

我正在尝试使用 vlabsmediaBundle 在一个实体上制作多个上传文件。 所以我有一则广告可以有很多图片,但每张图片只能与一个广告链接。 我遵循了捆绑包的教程(tuto),但出现错误。

此处:

Entity Image.php

namespace MDB\PlatformBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Vlabs\MediaBundle\Entity\BaseFile as VlabsFile;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Image
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Image extends VlabsFile {



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

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

    /**
     * @ORM\ManyToOne(targetEntity="MDB\AnnonceBundle\Entity\Annonce", inversedBy="images")
     * @ORM\JoinColumn(nullable=true)
     */
    private $annonce;

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

    /**
     * Set url
     *
     * @param string $url
     * @return Image
     */
    public function setUrl($url) {
        $this->url = $url;

        return $this;
    }

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

    /**
     * Set alt
     *
     * @param string $alt
     * @return Image
     */
    public function setAlt($alt) {
        $this->alt = $alt;

        return $this;
    }

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

    public function getAnnonce() {
        return $this->annonce;
    }

    public function setAnnonce($annonce) {
        $this->annonce = $annonce;
    }

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

    /**
     * Set path
     *
     * @param string $path
     * @return Image
     */
    public function setPath($path) {
        $this->path = $path;

        return $this;
    }

    /**

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

}

Annonce.php (advert enity)

<?php

namespace MDB\AnnonceBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use Vlabs\MediaBundle\Annotation\Vlabs;

/**
 * Annonce
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="MDB\AnnonceBundle\Entity\AnnonceRepository")
 */
class Annonce {

    public function __construct() {
        $this->date = new \Datetime();
        $this->categories = new ArrayCollection();
        $this->images = new ArrayCollection();
    }

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

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

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

    /**
     * @ORM\Column(name="date", type="date")
     */
    private $date;

    /**
     * @var float
     *
     * @ORM\Column(name="prix", type="float")
     */
    private $prix;

    /**
     * @ORM\ManyToOne(targetEntity="MDB\AdresseBundle\Entity\Ville", inversedBy="annonces")
     */
    private $ville;

    /**
     * @ORM\ManyToMany(targetEntity="MDB\AnnonceBundle\Entity\Category", cascade={"persist"})
     */
    private $categories;

    /**
     * @ORM\ManyToMany(targetEntity="MDB\UserBundle\Entity\User")
     *
     */
    private $wishlist;

    /**
     * @var boolean
     *
     * @ORM\Column(name="telAppear", type="boolean")
     */
    private $telAppear;

    /**
     * @ORM\ManyToOne(targetEntity="MDB\UserBundle\Entity\User", inversedBy="annonces")
     * @ORM\JoinColumn(nullable=false)
     */
    private $user;

    /**
     *  @var VlabsFile
     * @ORM\OneToMany(targetEntity="MDB\PlatformBundle\Entity\Image", mappedBy="annonce")
     * @Vlabs\Media(identifier="image_entity", upload_dir="files/images")
     * @Assert\Valid()
     */
    private $images;

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

    /**
     * Set titre
     *
     * @param string $titre
     * @return Annonce
     */
    public function setTitre($titre) {
        $this->titre = $titre;

        return $this;
    }

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

    /**
     * Set description
     *
     * @param string $description
     * @return Annonce
     */
    public function setDescription($description) {
        $this->description = $description;

        return $this;
    }

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

    /**
     * Set prix
     *
     * @param float $prix
     * @return Annonce
     */
    public function setPrix($prix) {
        $this->prix = $prix;

        return $this;
    }

    /**
     * Get prix
     *
     * @return float 
     */
    public function getPrix() {
        return $this->prix;
    }

    public function addCategory(Category $category) {
        // Ici, on utilise l'ArrayCollection vraiment comme un tableau
        $this->categories[] = $category;

        return $this;
    }

    public function removeCategory(Category $category) {
        $this->categories->removeElement($category);
    }

    public function getCategories() {
        return $this->categories;
    }

    public function getDate() {
        return $this->date;
    }

    public function setDate($date) {
        $this->date = $date;
    }

    public function getWishlist() {
        return $this->wishlist;
    }

    public function setWishlist($wishlist) {
        $this->wishlist = $wishlist;
    }

    public function getVille() {
        return $this->ville;
    }

    public function setVille($ville) {
        $this->ville = $ville;
    }

    public function getTelAppear() {
        return $this->telAppear;
    }

    public function setTelAppear($telAppear) {
        $this->telAppear = $telAppear;
    }

    public function getUser() {
        return $this->user;
    }

    public function setUser($user) {
        $this->user = $user;
    }

    public function addImage(Image $image) {
        $this->images[] = $image;
        $image->setUser($this);
        return $this;
    }

    public function removeImage(Image $image) {
        $this->images->removeElement($image);
    }

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

}

config.yml

vlabs_media:
    image_cache:
        cache_dir: files/c
    mapping: 
        image_entity:
          class: MDB\PlatformBundle\Entity\Image

AnnonceSellType.php

<?php

namespace MDB\AnnonceBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

use MDB\PlatformBundle\Form\ImageType;


class AnnonceSellType extends AbstractType {


 private $arrayListCat;

    public function __construct( $arrayListCat)
    {

        $this->arrayListCat = $arrayListCat;
    }
     /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
                ->remove('wishlist')
                ->remove('date')
                ->remove('images')
                ->add('titre')
                ->add('description', 'textarea')
                ->add('prix')
                ->add('categories', 'choice', array(
                    'choices' => $this->arrayListCat,
                    'multiple' => true,
                    'mapped'=>false,
                ))
                //->add('images', 'collection', array('type' => new ImageType()))
                 ->add('image', 'vlabs_file', array(
                'required' => false
            ))
                ;
    }

    /**
     * @return string
     */
    public function getName() {
        return 'mdb_annoncebundle_annonce_sell';
    }

    public function getParent() {
        return new AnnonceType();
    }

}

我有以下错误:Catchable Fatal Error: Argument 1 passed to Vlabs\MediaBundle\EventListener\BaseFileListener::preSetData() must be an instance of Symfony\Component\Form\Event\DataEvent, instance of Symfony\Component\Form\FormEvent given in C:\wamp\www\Mdb\vendor\vlabs\media-bundle\Vlabs\MediaBundle\EventListener\BaseFileListener.php line 59

我试图改变这一行:

->add('image', 'vlabs_file', array(
                'required' => false
            ))

与:

->add('images', 'collection', array('type' => 'vlabs_file'))

但我只有在这种情况下出现的图像标签

如果有人有想法?

我对您的问题进行了一些调查,Vlabs Media Bundle 似乎无人维护。 PRE_SET_DATA 事件 (Vlabs\MediaBundle\EventListener\BaseFileListener::preSetData()) 上的表单侦听器与新版本的 Symfony 不兼容(它期望 DataEvent 作为参数而不是 FormEvent)。他们 fixed it but after 1.1.1 release(您可以比较链接中的日期)。如果真的想尝试一下,请更改 composer.json 和 运行 作曲家更新。我不能保证不会有其他可能的问题。

composer.json

"vlabs/media-bundle": "dev-master"

库更新

composer update vlabs/media-bundle