1
我想知道如何在不使用收集字段类型的情况下创建多个文件上传。事情是:我有一个班,我需要链接图片。我不需要这些图像中的任何描述和其他类型的信息,所以我只是在主实体中创建了额外的字段。然后,添加使用Symfony2上传和删除多个文件
->add('files', 'file', array(
'label' => 'Images',
'required' => false,
'attr' => array(
'accept' => 'image/*',
'multiple' => true
)
它
的窗体类和这样的:
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (!empty($this->files)) {
if (!empty($this->images)) {
$this->insertingKey = $this->images->count();
}
foreach ($this->files as $file) {
$imageName = uniqid('entity_') . '_' . date('Y-m-d_H:i') . '.' . $file->guessExtension();
if ($this->images === null) {
$this->images = new ArrayCollection();
}
$this->images->add($imageName);
}
}
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (empty($this->files)) {
return;
}
if ($this->insertingKey) {
foreach ($this->files as $file) {
$file->move($this->getUploadRootDir(), $this->images[ $this->insertingKey++ ]);
}
} else {
foreach ($this->files as $key => $file) {
$file->move($this->getUploadRootDir(), $this->images[ $key ]);
}
}
}
的学说事件在我的实体类,当然我做喜欢和数组文件归档工作。 但我的问题是 - 我无法第二次添加图像。例如,当我已经上传了一些图片并决定上传更多图片时,他们会实际上传图片(我可以在我的项目目录中看到它们),但是我的网页上没有任何更改。
你能给我些建议吗?或者,也许还有其他方法通过上传许多文件并同时只有一个表单域?万分感谢。