symfony2图像多重上传

时间:2015-03-08 22:38:30

标签: php symfony symfony-2.6

我试图找出如何进行多个文件上传,但没有运气。我还没有得到太多关于此的信息..也许有人会帮助我吗? :d

标准:

我知道在我的表格类型中,我应该使用倍数作为字段;但是当我添加它时,它会给我这个错误。

  

捕获致命错误:参数1传递给   PhotoGalleryBundle \ Entity \ Image :: setFile()必须是。的实例   Symfony \ Component \ HttpFoundation \ File \ UploadedFile,给出数组,   呼唤   /home/action/workspace/www/DecorInterior/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php   在442行并定义       图像实体

这是我在PHP中的代码:

<?php

namespace PhotoGalleryBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Gedmo\Mapping\Annotation as Gedmo;

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

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

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

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

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="imageUploadedDate", type="datetime")
     * @Gedmo\Timestampable(on="create")
     */
    private $imageUploadedDate;

    /**
     * @Assert\File(maxSize="6000000")
     */
    private $file;

    /**
     * @ORM\ManyToOne(targetEntity="Album", inversedBy="images")
     * @ORM\JoinColumn(name="album", referencedColumnName="id")
     */
    private $album;

    private $temp;

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

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

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

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

    /**
     * Set file.
     * 
     * @param UploadedFile $file
     */
    public function setFile(UploadedFile $file = null) {
        $this->file = $file;
        // check if we have an old image path
        if (isset($this->imageFilePath)) {
            // store the old name to delete after the update
            $this->temp = $this->imageFilePath;
            $this->imageFilePath = null;
        } else {
            $this->imageFilePath = 'initial';
        }
    }

    /**
     * @ORM\PrePersist()
     * @ORM\PreUpdate
     */
    public function preUpload() {
        if (null !== $this->getFile()) {
            // do whatever you want to generate a unique name
            $fileName = sha1(uniqid(mt_rand(), true));
            $this->imageFilePath = $fileName . '.' . $this->getFile()->guessExtension();
        }
    }

    /**
     * @ORM\PostPersist()
     * @ORM\PostUpdate
     */
    public function upload() {
        if (null === $this->getFile()) {
            return;
        }

        // if there is an error when moving the file, an exception will
        // be automatically thrown by move(). This will properly prevent
        // the entity from being persisted to the database on error
        $this->getFile()->move($this->getUploadRootDir(), $this->imageFilePath);

        // check if we have an old image
        if (isset($this->temp)) {
            // delete the old image
            unlink($this->getUploadRootDir() . '/' . $this->temp);
            // clear the temp image path
            $this->temp = null;
        }

        $this->imageFilePath = null;
    }

    /**
     * @ORM\PostRemove()
     */
    public function removeUpload() {
        $file = $this->getAbsolutePath();
        if ($file) {
            unlink($file);
        }
    }

    /**
     * Get file.
     * 
     * @return UploadedFile
     */
    public function getFile() {
        return $this->file;
    }

    public function getAbsolutePath() {
        return null === $this->imageFilePath
            ? null
            : $this->getUploadRootDir() . '/' . $this->imageFilePath;
    }

    public function getWebPath() {
        return null === $this->imageFilePath
            ? null
            : $this->getUploadDir() . '/' . $this->imageFilePath;
    }
    public function getUploadRootDir() {
        // the absolute path where uploaded
        // documents should be saved
        return __DIR__.'/../../../web/' . $this->getUploadDir();
    }

    public function getUploadDir() {
        // get rid of the __DIR__ so it doesn't screw up
        // when displaying uploaded doc/image in the view
        return 'uploads/images';
    }

    /**
     * Set album
     *
     * @param \PhotoGalleryBundle\Entity\Album $album
     * @return Image
     */
    public function setAlbum(\PhotoGalleryBundle\Entity\Album $album = null)
    {
        $this->album = $album;

        return $this;
    }

    /**
     * Get album
     *
     * @return \PhotoGalleryBundle\Entity\Album 
     */
    public function getAlbum()
    {
        return $this->album;
    }
}

将ImageType:

<?php

namespace PhotoGalleryBundle\Form;

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

class ImageType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('imageCaptation')
            ->add('imageName')
            ->add('file', 'file', array('multiple' => TRUE))
            ->add('album')
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'PhotoGalleryBundle\Entity\Image'
        ));
    }

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

1 个答案:

答案 0 :(得分:1)

在这种情况下,该值将是UploadedFile个对象的数组。鉴于您的类型包含多个文件但只有一个标题,名称等,我假设您需要重构Image以支持多个图像文件。在这种情况下,file属性更好地称为filessetFiles应该接受数组。

但是,如果您希望每个文件上传一个Image实体,请考虑改为实现collection

或者,您可以在操作中手动处理表单。例如:

public function uploadAction(Request $request)
{
    foreach ($request->files as $uploadedFile) {
        $uploadedFile = current($uploadedFile['file']);
        // Build Image using each uploaded file
    }
    ...