如何在更新sonata管理bundle中的值时编辑上传文件字段



当我从sonata admin bundle上传文件时,文件已上传,但当我编辑文件时,file字段也要求上传新文件。。请帮我选择上传文件的编辑选项。

class Adds {
use SymfonyComponentConfigDefinitionIntegerNode;
use DoctrineCommonCollectionsArrayCollection;
use GedmoMappingAnnotation as Gedmo;
use DoctrineORMMapping as ORM;
use SymfonyComponentHttpFoundationFileUploadedFile;
const SERVER_PATH_TO_IMAGE_FOLDER = 'uploads/';
..........

       /**
        * @var string
        *
        * @ORMColumn(name="file", type="string", length=255)
        * @AssertFile(maxSize="5000000")
        */
       private $file;
      public function getFile() {
    return $this->file;
       }
       public function setFile($file) {
    $this->file = $file;
       }
    ,............
     public function upload() {
    if (null === $this->getFile()) {
       return;
    }
    $this->getFile()->move(
    self::SERVER_PATH_TO_IMAGE_FOLDER, $this->getFile()->getClientOriginalName());
    $this->filename = $this->getFile()->getClientOriginalName();
    $this->setFile(null);
       }
       /**
        * upload the file to the server
        */
       public function lifecycleFileUpload() {
    $this->upload();
       }
       /**
        * Updates the hash value to force the preUpdate and postUpdate events to fire
        */
    public function refreshUpdated($file) {
       if (null === $file->getFile()) {
       return;
    }
         $file->getFile()->move(
         self::SERVER_PATH_TO_IMAGE_FOLDER, date('YmdHis') .                            $file->getFile()->getClientOriginalName());
    $file->filename = $file->getFile()->getClientOriginalName();
    $file->filename = date('YmdHis') . $file->filename;
    $this->setFile($file->filename);
    }
    ?>
    And the adAdmin file is
     <?php 
    ...............
    ................
    class AddsAdmin extends Admin {
    ............
    protected function configureFormFields(FormMapper $formMapper) {
             $formMapper->add('file', 'file', array('label' => 'File :', 'data_class' => null))
             ->end();
    }
    public function prePersist($ad) {
       $this->manageFileUpload($ad);
       if ($this->userLevel == Organization::LEVEL_CLIENT) {
       $user = $this->securityContext->getToken()->getUser();
       $ad->setOrganization($user->getOrganization());
    }
    }
       public function preUpdate($ad) {
       $this->manageFileUpload($ad);
       }
       private function manageFileUpload($ad) {
        if ($ad->getFile()) {
        $ad->refreshUpdated($ad);
       }
       }

只需检查字段的类型;

所以对于preUpdate/prePersist调用您的句柄:

public function preUpdate($object)
{
    $this->handleUploads($object);
}
public function prePersist($object)
{
    $this->handleUploads($object);
}

并且在手柄中只检查:

       if ($file->getFile() instanceof UploadedFile && $file->getFile()->isValid()) {
            $targetFilename = md5(rand() . time()) . '.' . $file->getFile()->getClientOriginalExtension();
            $file->getFile()->move($uploadDirectory, $targetFilename);
            $file->setFile('uploads/' . $targetFilename);
        } 

最新更新