2

I am trying to upload a file based on the VichUploaderBundle. I got this error when implementing it:

ContextErrorException: Catchable Fatal Error: Argument 1 passed to Minn\AdsBundle\Entity\MotorsAdsFile::setFile() must be an instance of Symfony\Component\HttpFoundation\File\File, string given,... 

I know that the problem is the Class of the given argument in the method setFile(). But, this I have found in the documentation on github.

So, this is what I have done & I hope I will solve the problem!

configuration of the bundle

vich_uploader:
    db_driver: orm # or mongodb or propel or phpcr
    mappings:
        motors_files:
            uri_prefix:         /files/motors
            upload_destination: %kernel.root_dir%/../web/files/motors
            namer: vich_uploader.namer_origname
            delete_on_remove: true

Definition of my File entity

<?php

namespace Minn\AdsBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
 * @ORM\Entity
 * @Vich\Uploadable
 */
class MotorsAdsFile {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    public $id;

    /**
     * @Assert\File(
     *     maxSize="5M",
     *     mimeTypes={"image/png", "image/jpeg"}
     * )
     * @Vich\UploadableField(mapping="motors_files", fileNameProperty="file")
     * note: This is not a mapped field of entity metadata, just a simple property.
     * @var File $file
     */
    protected $file;

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

    /**
     * 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 $file
     */
    public function setFile(File $file) {
        $this->file = $file;
    }

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

    /**
     * @param string $name
     */
    public function setName($name) {
        $this->name = $name;
    }

    /**
     * @return string
     */
    public function getName() {
        return $this->name;
    }

}

Twig part for the form

<form action="{{ path('minn_ads_motors_test6') }}" {{ form_enctype(form) }} method="POST">
    {{ form_widget(form) }}
    <div>
        <input type="submit" value="do it with VICH" />
    </div>
</form>

my action() in my controller

public function motorstest6Action(Request $request) {
    $document = new MotorsAdsFile();
    $form = $this->createFormBuilder($document)
            ->add('name')
            ->add('file')
            ->getForm();

    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($document);
        $em->flush();

        return $this->redirect($this->generateUrl('minn_ads_default_index'));
    }
    return $this->render('MinnAdsBundle:Motors:adddoc1.html.twig', array(
                'form' => $form->createView()));
}

2 Answers 2

2

Your entity isn't configured properly. In order for this bundle to work, the entity must have two fields to represent the file: one to store the File object (not mapped by doctrine) and another one to store its name (as a string, mapped by doctrine). Your entity only has the first field.

Here is how you should have defined it:

<?php

namespace Minn\AdsBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
 * @ORM\Entity
 * @Vich\Uploadable
 */
class MotorsAdsFile {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    public $id;

    /**
     * @Assert\File(
     *     maxSize="5M",
     *     mimeTypes={"image/png", "image/jpeg"}
     * )
     * @Vich\UploadableField(mapping="motors_files", fileNameProperty="filename")
     * note: This is not a mapped field of entity metadata, just a simple property.
     * @var File $file
     */
    protected $file;

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

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

    /**
     * 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 $file
     */
    public function setFile(File $file) {
        $this->file = $file;
    }

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

    /**
     * @param string $filenname
     */
    public function setFilename($filename) {
        $this->filename = $filename;
    }

    /**
     * @return string
     */
    public function getFilename() {
        return $this->file;
    }

    /**
     * @param string $name
     */
    public function setName($name) {
        $this->name = $name;
    }

    /**
     * @return string
     */
    public function getName() {
        return $this->name;
    }
}

Note the new filename field and the new value for the filenameProperty option in the UploadableField annotation.

Sign up to request clarification or add additional context in comments.

Comments

0

You're done! Now create a form with an imageFile field that uses the file type.

https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/usage.md#that-was-it

So you might want to try this

        ->add('file', 'file')

Instead of

        ->add('file')

As your field isn't mapped the FormBuilder probably puts it as string by default.

1 Comment

I do still have the same problem!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.