2016-08-04 92 views
3

數據夾具基本Symfony的3個應用程序具有VichUploader bundle安裝和配置,用於上傳文件對一個實體。與VichUploader束在Symfony3

如何將文件附加到這個實體在我的ORM數據燈具?


有一個question for this problem in Symfony 2我試過這段代碼。夾具加載沒有錯誤,但「上傳」的文件不會複製到最終目的地。

我可以手動做到這一點,我的最新嘗試是這樣的:

<?php 
// src/AppBundle/DataFixtures/ORM/LoadCourseData.php 

namespace AppBundle\DataFixtures\ORM; 

use Doctrine\Common\DataFixtures\OrderedFixtureInterface; 
use Doctrine\Common\DataFixtures\AbstractFixture; 
use Doctrine\Common\Persistence\ObjectManager; 
use Symfony\Component\HttpFoundation\File\UploadedFile; 
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser; 
use AppBundle\Entity\Course; 

class LoadCourseData extends AbstractFixture implements OrderedFixtureInterface 
{ 
    public function load(ObjectManager $manager) 
    { 
     $course = new Course(); 
     $course->setName('How to make data fixtures in Symfony lol') 
      ->setAuthor($this->getReference('admin-user')) 
      ->setSummary('You\'ll know when I know!') 
      ->setLicense($this->getReference('cc-by')) 
      ->setDifficulty(1); 

     // Persist to generate slug (to be used for uploads) 
     $manager->persist($course); 

     // Attach file 

     // I copy it to a temporary directory as per the Symfony2 answer 
     copy(__DIR__.'/../Files/kitten.jpg', '/tmp/kitten.jpg'); 

     // These are filled in for completeness 
     $mimetype = MimeTypeGuesser::getInstance()->guess('/tmp/kitten.jpg'); 
     $size  = filesize('/tmp/kitten.jpg'); 

     // Create a new UploadedFile 
     $file = new UploadedFile('/tmp/kitten.jpg', 'kitten.jpg', $mimetype, $size, null, true); 

     // I have to move it manually? 
     // Plus how can I take advantage of the VichUploader namer? 
     $file->move(__DIR__.'/../../../../web/img/upload/course/'); 

     // Apply to entity 
     $course->setImageFile($file); 

     // Persist 
     $manager->persist($course); 
     $manager->flush(); 
    } 

    public function getOrder() 
    { 
     return 4; 
    } 
} 

我的解決方案仍然需要大量的工作!我吠叫錯了樹嗎?

  • 上傳路徑已在config.yml中定義。我應該在那裏訪問,或將其轉換爲參數
  • 需要更多的錯誤檢查和異常拋出
  • 將文件複製到目標,但文件名是錯誤的,因爲它不使用配置VichUploader命名器
  • 它應該住在其他地方,可能作爲一種服務?

回答

1

我發現在GitHub上不太好VichUploader文檔,所以我建議你使用的Symfony文檔: http://symfony.com/doc/current/controller/upload_file.html

我創建了一個FileUploader.php在src /的appbundle像這樣:

<?php 

// src/AppBundle/FileUploader.php 
namespace AppBundle; 

use Symfony\Component\HttpFoundation\File\UploadedFile; 

class FileUploader 
{ 
    private $targetDir; 

    public function __construct($targetDir) 
    { 
     $this->targetDir = $targetDir; 
    } 

    public function upload(UploadedFile $file, $path, $name) 
    { 
     $fileName = $name.'.'.$file->guessExtension(); 

     $file->move($this->targetDir.$path, $fileName); 

     return $fileName; 
    } 
} 

這使您可以指定文件,路徑和名稱。

然後在你的應用程序/配置/ service.yml文件,添加此引用它:

services: 
    app.attachment_uploader: 
     class: AppBundle\FileUploader 
     arguments: ['%document_directory%'] 

然後,你需要在你的應用程序/配置/ parameters.yml文件中指定%document_directory%:

parameters: 
... 
    document_directory: documents 

就我而言,上述'文件'文件夾位於web文件夾下,所以路徑是'web/documents'。我想把所有東西都放在同一個地方。

然後,我使用FileUploader中的任何控制器,像這樣:

... 
->add('catalog', FileType::class, array(   // Docs 
     'label' => 'Catalog Course Description ', 
     'required' => false, 
     'attr' => array(
       'accept' => 'image/*,application/pdf,text/plain,application/msword', 
       'onchange' => "checkFileSize('form_catalog')" 
     ), 
)) 
... 

$file = $form->get("catalog")->getData(); 
$fileName = $this->get('app.attachment_uploader')->upload($file, 
     '/'.$pet->getStudent()->getBanId(), 
     $pet->getCourse()->getCorTitle().'_'.'Catalog_Course_Desc' 
... 

$att_cat->setDocument($fileName); 
$em->persist($att_cat); 
$em->flush(); 

凡在上面的代碼中,我發現了「目錄」文件按鈕的數據,這是該文件。然後調用指定文件和路徑和文件名參數的'upload'方法。然後稍後堅持實體($ att_cat)。

以上使用非常簡單。