Vichuploaderbundle: writing fixtures

Created on 16 Nov 2012  路  4Comments  路  Source: dustin10/VichUploaderBundle

config:
...
 mappings:
        product_image:
           uri_prefix: /images/products
            upload_destination: %kernel.root_dir%/../web/images/products

Entity:

     /**
     * @Vich\UploadableField(mapping="product_image", fileNameProperty="productImage")
     */
    protected $file;

I try this:

use    Symfony\Component\HttpFoundation\File\File;
....
        $path = '/tmp/someImage.jpg';
        $imageFile = new File($path);
        $entity->setFile($imageFile);

but it does not work :(

Any ideas?

Most helpful comment

Hey, simple exemple ;)

<?php
namespace AppBundle\DataFixtures\ORM;

use AppBundle\Entity\Document;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class LoadDocumentsData extends AbstractFixture implements OrderedFixtureInterface 
{
    public function load(ObjectManager $manager)  {

        $src = __DIR__."/../Resources/doc/mydocument.pdf";

        $file = new UploadedFile(
            $src,
            'mydocument.pdf',
            'application/pdf',
            filesize($src),
            null,
            true //  Set test mode true !!! " Local files are used in test mode hence the code should not enforce HTTP uploads."
        );

        $document = new Document();
        $document->setDocumentFile($file);

        $manager->persist($document);
        $manager->flush();
    }
}

All 4 comments

You should put 'someImage.jpg' in your filename property which is the one that will be persisted eventually.

ok, but it does not upload file to destination directory which set in "upload_destination"
Have I upload this file manually?

You can try to set an UploadedFile instance instead of a File. To construct an UploadedFile you'll need to define some extra information (see the constructor of UploadedFile for details).

The only alternative would be to put the file directly into the upload destination.

Hey, simple exemple ;)

<?php
namespace AppBundle\DataFixtures\ORM;

use AppBundle\Entity\Document;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class LoadDocumentsData extends AbstractFixture implements OrderedFixtureInterface 
{
    public function load(ObjectManager $manager)  {

        $src = __DIR__."/../Resources/doc/mydocument.pdf";

        $file = new UploadedFile(
            $src,
            'mydocument.pdf',
            'application/pdf',
            filesize($src),
            null,
            true //  Set test mode true !!! " Local files are used in test mode hence the code should not enforce HTTP uploads."
        );

        $document = new Document();
        $document->setDocumentFile($file);

        $manager->persist($document);
        $manager->flush();
    }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

jcg678 picture jcg678  路  6Comments

manguecreative picture manguecreative  路  4Comments

nobady90 picture nobady90  路  3Comments

fleskalebas picture fleskalebas  路  6Comments

ngilain picture ngilain  路  5Comments