我正在尝试将 VichImageBundle 与 Symfony3 一起使用,目前还没有关于 S3 中新增的 FormType::class 调用与 Vich Image Bundle 结合使用的文档。除了移动部分之外,我的一切都正常。
我在项目内部创建了一个文件夹结构:web/images/aod_images
我在 yml 文件中设置了如下配置:
vich_uploader:
db_driver: orm
mappings:
aod_images:
uri_prefix: /images/aod_images
upload_destination: %kernel.root_dir%/../web/images/aod_images
实体属性在这里:(我有 getter 和 setter)
/**
* @Vich\UploadableField(mapping="aod_images", fileNameProperty="dailyChartName")
*
* @var File
*/
private $dailyChart;
/**
* @ORM\Column(type="string", length=255)
*
* @var string
*/
private $dailyChartName;
/**
* @Vich\UploadableField(mapping="aod_images", fileNameProperty="twohourChartName")
*
* @var File
*/
private $twohourChart;
/**
* @ORM\Column(type="string", length=255)
*
* @var string
*/
private $twohourChartName;
我在 Controller 类中调用适当的设置时间方法,特别是在 editAction 和 newAction Controller 上:
$analysis->setUpdatedAt(new \DateTime('now'));
到目前为止,在该实体的 FormType 类中,我有以下内容:
->add('dailyChartName', FileType::class, array())
->add('twohourChartName', FileType::class, array())
我不确定要为每个图像实例传递哪些选项到 ->add 方法。
应用程序正在正确构建表单。
当我对 dev.log 执行 tail -f 操作时,我收到以下消息作为doctrine.DEBUG channel 的一部分:
(Symfony\\Component\\HttpFoundation\\File\\UploadedFile: /Applications/MAMP/tmp/php/phpEz6kJO)","13":"[object] (Symfony\\Component\\HttpFoundation\\File\\UploadedFile: /Applications/MAMP/tmp/php/phpVkbZgi)
图像名称属性与我在 config.yml 文件中映射的内容不匹配。相反,该路径反射(reflect)了我的本地计算机到内部 php tmp 目录的路由。使用 server:run 命令后,我从 127.0.0.1:8000 运行此命令。
当我查看该目录时,图像没有被移动到那里。
正在将(不正确的)图像名称复制到数据库中。但是图像的路径是空的(路径不正确)。另外正确的路径/目录也是空的。
问候!
编辑
根据 Matteo 的建议,我对 FormType.php 文件进行了以下更改。
->add('dailyChart', VichFileType::class, array(
'required' => TRUE,
))
->add('twohourChart', VichFileType::class, array(
'required' => TRUE,
))
编辑2
为了使我的实现更紧密地遵循文档,我按照文档建议将 updateAt 集放入实体内。
我的设置方法看起来像这样:
public function setDailyChart(\Symfony\Component\HttpFoundation\File\UploadedFile $dailyChart)
{
$this->dailyChart = $dailyChart;
if ($dailyChart) {
$this->updatedAt = new \DateTime('now');
}
return $this;
}
我明确设置了完整路径的打字提示。现在我收到以下错误。
AppBundle\Entity\MTI_Cart\AodTechnicalAnalysis::setDailyChart() must be an instance of Symfony\Component\HttpFoundation\File\UploadedFile, instance of Symfony\Component\HttpFoundation\File\File given
最佳答案
要将此 bundle 与 symfony3 一起使用,您必须使用表单类型:VichFileType
,如文档 here 中所述。 ,例如:
// ...
use Vich\UploaderBundle\Form\Type\VichFileType;
// ...
public function buildForm(FormBuilderInterface $builder, array $options)
{
// ...
$builder->add('image', VichFileType::class, array(
'required' => false,
'allow_delete' => true, // not mandatory, default is true
'download_link' => true, // not mandatory, default is true
));
}
所以在你的 formType 类中试试这个:
use Vich\UploaderBundle\Form\Type\VichFileType;
//..
->add('dailyChartName', VichFileType::class, array())
->add('twohourChartName', VichFileType::class, array())
希望这有帮助
关于Symfony 3 VichUploaderBundle 未将图像移动到正确的目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36725887/