drdyszuy488152 2017-08-17 00:44
浏览 105
已采纳

EasyAdminBundle和VichUploaderBundle - 错误:给出类型“”,“Symfony \ Component \ HttpFoundation \ File \ UploadedFile”的预期参数

I'm using EasyAdminBundle for entity management and to upload images I want to useVichUploaderBundle.

Following the documentation configure the Bundle: https://github.com/javiereguiluz/EasyAdminBundle/blob/master/Resources/doc/integration/vichuploaderbundle.rst

I do not use annotations but yml as described in the documentation: https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/mapping/yaml.md

My code looks like this:

//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' }

The yml configuration file:

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

Add to Entity

//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;
}

Also add this code (I'm not sure if it's correct)

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

Can anyone give me some idea to fix it?

  • 写回答

1条回答 默认 最新

  • doudeng2184 2017-08-20 16:45
    关注

    The solution was quite simple. The error occurs because the use are placed before thenamespace in the controller.

    namespace BackendBundle\Entity;
    

    Regards

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏