2013-11-22 60 views
0

雖然我上傳文件從奏鳴曲管理捆綁文件上傳,但是當我編輯文件的文件字段也要求上傳新文件..請幫助我的文件上傳的編輯選項。如何在更新奏鳴曲管理包中的值時編輯上傳文件字段?

class Adds { 
use Symfony\Component\Config\Definition\IntegerNode; 
use Doctrine\Common\Collections\ArrayCollection; 
use Gedmo\Mapping\Annotation as Gedmo; 
use Doctrine\ORM\Mapping as ORM; 
use Symfony\Component\HttpFoundation\File\UploadedFile; 
const SERVER_PATH_TO_IMAGE_FOLDER = 'uploads/'; 
.......... 


     /** 
     * @var string 
     * 
     * @ORM\Column(name="file", type="string", length=255) 
     * @Assert\File(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); 
     } 
     } 

回答

0

只要檢查字段的類型;

SO用於更新前/ 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); 

     } 
相關問題