Rick Mason
Rick Mason

Reputation: 311

Symfony 3 VichUploaderBundle not moving images to the correct directory

I am attempting to use The VichImageBundle with Symfony3, there is no documentation as of yet for the FormType::class call that is new to S3 in conjunction with the Vich Image Bundle. I have everything working except for the moving part.

I have made a folder structure here inside the project here: web/images/aod_images

I have set the configuration in the yml file like this:

vich_uploader:
    db_driver: orm 

    mappings:      
        aod_images:
            uri_prefix:         /images/aod_images
            upload_destination: %kernel.root_dir%/../web/images/aod_images

The entity properties are here: (I have the getters and setters in place)

/**
 * @Vich\UploadableField(mapping="aod_images", fileNameProperty="dailyChartName")
 * 
 * @var File
 */
private $dailyChart;

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

/**
 * @Vich\UploadableField(mapping="aod_images", fileNameProperty="twohourChartName")
 * 
 * @var File
 */
private $twohourChart;

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

I am calling a set time method as appropriate in the controller class, specifically on the editAction and the newAction controllers:

$analysis->setUpdatedAt(new \DateTime('now'));

In the FormType class for this entity I have the following so far:

->add('dailyChartName', FileType::class, array())
->add('twohourChartName', FileType::class, array())

I'm not sure what options to pass to the ->add method yet for each image instance.

The application is building the forms correctly.

When I do a tail -f the dev.log I get the following message as part of the doctrine.DEBUG channel:

(Symfony\\Component\\HttpFoundation\\File\\UploadedFile: /Applications/MAMP/tmp/php/phpEz6kJO)","13":"[object] (Symfony\\Component\\HttpFoundation\\File\\UploadedFile: /Applications/MAMP/tmp/php/phpVkbZgi)

The image name property does not match what I have mapped in the config.yml file. Instead the path reflects my local machines route to internal php tmp directory. I'm running this from 127.0.0.1:8000 after using the server:run command.

When I look in that directory the image has NOT been moved there.

The (incorrect)image name is being copied to the database. However the path to the image is empty (incorrect path). In addition the correct path/directory is also empty.

Regards!

EDIT

As per suggestion by Matteo I have made the following change to the FormType.php file.

->add('dailyChart', VichFileType::class, array(
            'required' => TRUE,
        ))
->add('twohourChart', VichFileType::class, array(
            'required' => TRUE,            
        ))

EDIT 2

In an attempt to make my implementation to adhere more closely to the documentation I put my updatedAt set inside the entity as per docs suggestion.

My set methods look something like this:

public function setDailyChart(\Symfony\Component\HttpFoundation\File\UploadedFile $dailyChart)
{
    $this->dailyChart = $dailyChart;
        if ($dailyChart) {
            $this->updatedAt = new \DateTime('now');
        }
    return $this;
}

I explicitly set typehinting with a full path. Now I receive the following error.

AppBundle\Entity\MTI_Cart\AodTechnicalAnalysis::setDailyChart() must be an instance of Symfony\Component\HttpFoundation\File\UploadedFile, instance of Symfony\Component\HttpFoundation\File\File given

Upvotes: 2

Views: 2833

Answers (1)

Matteo
Matteo

Reputation: 39410

For use this bundle with symfony3 you must use the form type: VichFileType as described in the doc here, as example:

// ...
use Vich\UploaderBundle\Form\Type\VichFileType;

// ...

public function buildForm(FormBuilderInterface $builder, array $options)
{
    // ...

    $builder->add('image', VichFileType::class, array(
        'required'      => false,
        'allow_delete'  => true, // not mandatory, default is true
        'download_link' => true, // not mandatory, default is true
    ));
}

So try this in your formType class:

use Vich\UploaderBundle\Form\Type\VichFileType;
//..
->add('dailyChartName', VichFileType::class, array())
->add('twohourChartName', VichFileType::class, array())

Hope this help

Upvotes: 2

Related Questions