【问题标题】:EasyAdminBundle and VichUploaderBundle - Error: The expected argument of type"", "Symfony\Component\HttpFoundation\File\UploadedFile" givenEasyAdminBundle 和 VichUploaderBundle - 错误:给定类型“”、“Symfony\Component\HttpFoundation\File\UploadedFile”的预期参数
【发布时间】:2017-08-17 00:44:24
【问题描述】:

我正在使用EasyAdminBundle 进行实体管理和上传我想使用VichUploaderBundle 的图像。

按照文档配置捆绑包: https://github.com/javiereguiluz/EasyAdminBundle/blob/master/Resources/doc/integration/vichuploaderbundle.rst

我不使用annotations,而是使用yml,如文档中所述: https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/mapping/yaml.md

我的代码如下所示:

//app/config/config.yml
vich_uploader:
db_driver: orm
mappings:
    torneo_images:
        uri_prefix:         '%app.path.torneo_images%'
        upload_destination: '%kernel.root_dir%/../web/uploads/images/torneos'

 ..........
 easy_admin:
 form:
            fields:
                - logo
                - { property: 'imageFile', type: 'file' }

yml 配置文件:

//BackendBundle/Resources/config/doctrine/Torneos.orm.yml
......
    logo:
        type: string
        nullable: true
        length: 255
        options:
            fixed: false
    imageFile:
        mapping: torneo_images
        filename_property: logo

添加到实体

//BackendBundle/Entity/Torneos.orm.yml
use Doctrine\ORM\Mapping as ORM;

use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\PropertyMapping as Vich;

namespace BackendBundle\Entity;
.......
/**
 * @var string
 */
private $logo;
/**
 * @var File
 */
private $imageFile;
.......
/**
 * Set logo
 *
 * @param string $logo
 *
 * @return Torneos
 */
public function setLogo($logo)
{
    $this->logo = $logo;

    return $this;
}

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

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

    // VERY IMPORTANT:
    // 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
    //if ($image) {
        // if 'updatedAt' is not defined in your entity, use another property
    //    $this->updatedAt = new \DateTime('now');
    //}
    return $this;
}

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

同时添加此代码(我不确定它是否正确)

//BackendBundle/Resources/config/vich_uploader/Torneos.orm.yml
BackendBundle\Entity\Torneos:
imageFile:
    mapping: torneo_images
    filename_property: logo

谁能给我一些解决办法?

【问题讨论】:

    标签: php symfony vichuploaderbundle symfony2-easyadmin


    【解决方案1】:

    解决方案非常简单。 发生错误是因为use 在控制器中放在namespace 之前。

    namespace BackendBundle\Entity;
    

    问候

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-17
      • 1970-01-01
      • 2014-06-24
      • 2019-12-09
      • 1970-01-01
      • 1970-01-01
      • 2021-09-27
      • 1970-01-01
      相关资源
      最近更新 更多