开发者问题收集

VichUploaderBundle- 预期参数类型为“AppBundle\Entity\File”,给出了“Symfony\Component\HttpFoundation\File\UploadedFile”

2016-06-26
6138

我按照说明操作: https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/usage.md 但在保存用户实体时出现以下错误:

Expected argument of type "AppBundle\Entity\File", "Symfony\Component\HttpFoundation\File\UploadedFile" given

这是代码:

/**
 * @ORM\Entity
 * @ORM\Table(name="symfony_demo_user")
 * @Vich\Uploadable
 */
class User extends BaseUser implements UserInterface 
{
    /**
     * @Vich\UploadableField(mapping="avatar_image", fileNameProperty="avatarName")
     * 
     * @var File
     */
    private $avatarFile;

    /**
     * @ORM\Column(type="string", length=255, nullable=true, options={"default": 0})
     *
     * @var string
     */
    private $avatarName;    

    /**
     * @ORM\Column(type="datetime")
     *
     * @var \DateTime
     */
    private $updatedAt;
/**
 * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
 *
 * @return User
 */
public function setAvatarFile(File $image = null)
{
    $this->avatarFile = $image;

    if ($image) {
        // 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
        $this->updatedAt = new \DateTime('now');
    }

    return $this;
}

/**
 * @return File
 */
public function getAvatarFile()
{
    return $this->avatarFile;
}   

我的表单:

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Vich\UploaderBundle\Form\Type\VichImageType;

class ProfileFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        # no translation_domain means - app translations from ~/app/translations/
        $builder
            ->add('avatarFile', 'vich_image');
    }

您知道问题出在哪里吗?

1个回答

您在方法 setAvatarFile 中说您正在等待 File 类的项目。您尚未将任何用途设置为此类的命名空间,因此它会查找类 AppBundle\Entity\File

在文件顶部添加 use Symfony\Component\HttpFoundation\File\File

jobou
2016-06-26