Sonata Admin + Vich上传注入负载不工作

时间:2016-02-26 13:17:02

标签: symfony sonata-admin sonata vichuploaderbundle

我正在使用vich上传与sonata admin&我可以上传&删除文件 我的问题是我无法获取上传的文件信息

我使用vich uploader config“inject_on_load”为真

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

现在,当我将对象转储到控制器中时,我得到了具有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 ▶}
} 

但是我在sonata中加载文件postUpdate(movie)保存钩子我没有得到关于文件的信息

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

我得到了这个结果

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

这是我的电影实体课

<?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;
    }

}

这是我的管理类

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

我缺少什么可以帮助? 是否有任何替代方法将上传的文件信息注入实体对象

1 个答案:

答案 0 :(得分:0)

我们需要手动刷新doctrine对象以加载更新的属性(vich uploader通过事件将文件信息注入对象属性)

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