symfony - Symfony 4.2 VichUploaderBundle:文件未保存
问题描述
我在我的应用程序中使用VichUploaderBundle来上传文件。
问题是,即使我遵循了文档,文件也没有上传(即使看起来没问题,如配置文件所说),并且实体表中没有行。
这是我的vich_uploader.yaml:
vich_uploader:
db_driver: orm
mappings:
media:
uri_prefix: /media
upload_destination: '%kernel.project_dir%/public/media'
inject_on_load: false
delete_on_update: true
delete_on_remove: true
这是我的媒体实体:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity
* @Vich\Uploadable
*/
class Media
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* @Vich\UploadableField(mapping="media", fileNameProperty="imageName", size="imageSize")
*
* @var File
*/
private $imageFile;
/**
* @ORM\Column(type="string", length=255)
*
* @var string
*/
private $imageName;
/**
* @ORM\Column(type="integer")
*
* @var integer
*/
private $imageSize;
/**
* @ORM\Column(type="datetime")
*
* @var \DateTime
*/
private $updatedAt;
/**
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance
* of 'UploadedFile' is injected into this setter to trigger the update. If this
* bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
* must be able to accept an instance of 'File' as the bundle will inject one here
* during Doctrine hydration.
*
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $imageFile
*/
public function setImageFile(?File $imageFile = null): void
{
$this->imageFile = $imageFile;
if (null !== $imageFile) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->updatedAt = new \DateTimeImmutable();
}
}
public function getImageFile(): ?File
{
return $this->imageFile;
}
public function setImageName(?string $imageName): void
{
$this->imageName = $imageName;
}
public function getImageName(): ?string
{
return $this->imageName;
}
public function setImageSize(?int $imageSize): void
{
$this->imageSize = $imageSize;
}
public function getImageSize(): ?int
{
return $this->imageSize;
}
}
我只需要将文件存储在实体中,文件与其他实体之间没有关系。
这是我的媒体类型
<?php
namespace App\Form;
use App\Entity\Media;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Vich\UploaderBundle\Form\Type\VichImageType;
class MediaType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('imageFile', VichImageType::class, [
'required' => false,
'allow_delete' => true,
'download_uri' => true
])
->add('submit', SubmitType::class, [
'attr' => [
'class' => 'btn btn-success'
],
'label' => 'Upload File'
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Media::class,
]);
}
}
表单由这个 Twig 模板处理:
<div class="container-fluid">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel">
<div class="panel-body">
{{ form_start(form) }}
{{ form_widget(form.imageFile) }}
</div>
<div class="panel-footer">
{{ form_widget(form.submit) }}
{{ form_end(form) }}
</div>
</div>
</div>
</div>
</div>
解决方案
推荐阅读
- c# - 限制类实例化
- mysql - 如何获取特定日期范围的最新数据?
- python - 将表连接到栅格属性表以创建具有重新分类值 python 或 R 的新栅格
- html - 如何使用 css nth-child(-n+3) 仅显示列表中的前三个图像
- python - 随机值张量上的计算(例如 tf.greater 和 tf.cond)未按预期工作
- python - 许多属性的内联循环命名元组
- sql - 如何从 SQL 中带有 WHERE 条件的下一条记录中获取值?
- symfony - 如何在 Sonata configureListFields 中检索主题而不是行动?
- proxy - 使用 Burp 拦截 http 请求不起作用?
- docker - 如何对在 docker swarm 中创建的 docker 服务应用程序进行 nginx 反向代理?