2016-08-04 96 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)。

以上使用非常简单。