Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VichUploaderBundle- expected argument of type "AppBundle\Entity\File", "Symfony\Component\HttpFoundation\File\UploadedFile" given

I followed the instruction: https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/usage.md to the letter but I'm getting the following error while saving User entity:

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

Here's the code:

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

My Form:

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');
    }

Do you know there the problem is?

like image 480
Torpedr Avatar asked Jun 26 '16 16:06

Torpedr


1 Answers

You say in the method setAvatarFile that you are waiting for an item of class File. You have not setted any use to namespace this class so it looks for the class AppBundle\Entity\File

Add a use Symfony\Component\HttpFoundation\File\File at the top of your file.

like image 161
jobou Avatar answered Nov 05 '22 08:11

jobou