Sonata Admin + Vich Upload inject on load not working

…衆ロ難τιáo~ 提交于 2019-12-11 11:25:34

问题


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:


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


来源:https://stackoverflow.com/questions/35652759/sonata-admin-vich-upload-inject-on-load-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!