1
votes

i am using vich upload with sonata admin & i can able to upload & delete file my issue is i am not able to get uploaded file information

i used vich uploader config "inject_on_load" as true

vich_uploader:
    db_driver: orm # or mongodb or propel or phpcr
    mappings:
        small_image:
            uri_prefix:         /uploads/images/small
            upload_destination: %kernel.root_dir%/../web/uploads/images/small
            namer:              vich_uploader.namer_uniqid
            inject_on_load:     true
            delete_on_update:   true
            delete_on_remove:   true

now when i dump my object in controller i got object with File Inejcted Proprieties

Movie {#679 ▼
 -id: 7
 -featureImageFile: -featureImageFile: File {#771 ▼
    path: "C:\wamp/../web/uploads/images/feature"
    filename: "56cd61b786c57.jpg"
    basename: "56cd61b786c57.jpg"
    pathname: "C:\wamp\www\/uploads/images/feature\56cd61b786c57.jpg"
    extension: "jpg"
    realPath: "C:\wamp\www\uploads\images\feature\56cd61b786c57.jpg"
    aTime: 2016-02-24 08:54:30
    mTime: 2016-02-24 08:54:30
    cTime: 2016-02-24 08:54:30
    inode: 0
    size: 173519
    perms: 0100666
    owner: 0
    group: 0
    type: "file"
    writable: true
    readable: true
    executable: false
    file: true
    dir: false
    link: false
    linkTarget: "C:\wamp\...\images\feature\56cd61b786c57.jpg"
   }
 -featureImageName: "56cd61b786c57.jpg"
  #regions: PersistentCollection {#717 ▶}
  #genre: Genre {#739 ▶}
  #language: Language {#745 ▶}
} 

but i am loading file in sonata postUpdate(movie) save hook i am getting no information about file

public function postUpdate($movie){
    dump($movie); exit;
}

i got this result

Movie {#679 ▼
 -id: 7
 -featureImageFile: null
 -featureImageName: "56cd61b786c57.jpg"
  #regions: PersistentCollection {#717 ▶}
  #genre: Genre {#739 ▶}
  #language: Language {#745 ▶}
} 

here is my Movie Entity class

<?php

namespace Application\NS\AdminBundle\Entity;

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


/**
 * Movie
 *
 * @ORM\Table(name="movie")
 * @ORM\Entity(repositoryClass="Application\NS\AdminBundle\Repository\MovieRepository")
 * @Vich\Uploadable
 */
class Movie
{



    /**
     * @Vich\UploadableField(mapping="feature_image", fileNameProperty="featureImageName")
     * @var File
     */
    private $featureImageFile;

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

    /**
     * Set featureImageName
     * @param string $featureImageName
     * @return object
     */
    public function setFeatureImageName($featureImageName) {
        $this->featureImageName = $featureImageName;
        return $this;
    }

    /**
     * Get featureImageName
     * @return string
     */
    public function getFeatureImageName(){
        return $this->featureImageName;
    }

}

here is my admin class

/**
 * @param FormMapper $formMapper
 */
protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
     ->add('featureImageFile', 'vich_image', array( 'required' => false, 'allow_delete'  => true, 'download_link' => false))
}                

what i am missing can any one help ? is there any alternative method to inject uploaded file information into Entity Object

1
could you show your entity and admin class code?jjgarcía
@jjgarcía i have update my question with entity & admin class codeAnil Gupta
Is the dump samples that appears empty, is to update an item or create new? maybe a silly question :-) I can reproduce this dump, when I do an upgrade without selecting a file.jjgarcía
@jjgarcía thanks, but i found that we need to apply refresh on doctrine objectAnil Gupta

1 Answers

0
votes

we need to manually refresh the doctrine object to to load updated properties (vich uploader inject file information to object property via events)

 $this->getContainer()->get('doctrine.orm.entity_manager')->refresh($movie);