一个基本的Symfony 3应用程序安装并配置了VichUploader bundle,并带有一个用于上传文件的实体.
如何在我的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命名符
>它应该住在其他地方,可能作为服务?
解决方法:
我发现GitHub上的VichUploader文档不是很好,所以我建议你使用Symfony文档:
http://symfony.com/doc/current/controller/upload_file.html
我在src / AppBundle下创建了一个FileUploader.PHP,如下所示:
<?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;
}
}
然后在你的app / config / service.yml文件中添加它以引用它:
services:
app.attachment_uploader:
class: AppBundle\FileUploader
arguments: ['%document_directory%']
然后,您需要在app / config / parameters.yml文件中指定%document_directory%:
parameters:
...
document_directory: documents
在我的例子中,上面的’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).
以上使用非常简单.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。