1
컬렉션 필드 유형을 사용하지 않고 여러 파일을 업로드하는 방법을 궁금합니다. 문제는 : 클래스가 있고이 클래스를 사용하여 이미지를 링크해야합니다. 이 이미지에는 설명이나 다른 종류의 정보가 필요하지 않으므로 필자는 주체에 추가 필드를 만들었습니다. 내 엔티티 클래스의 교리 이벤트로Symfony2로 여러 파일 업로드 및 제거
/**
* @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 ]);
}
}
}
, 물론 내가 좋아하는 어레이 파일 제출 한 작품을 만들 : 그럼 거기에
->add('files', 'file', array(
'label' => 'Images',
'required' => false,
'attr' => array(
'accept' => 'image/*',
'multiple' => true
)
양식 클래스와이있어 추가합니다. 하지만 내 문제는 - 두 번째 이미지를 추가 할 수 없습니다. 예를 들어, 이미 이미지를 업로드하고 더 업로드하기로 결정한 경우 물리적으로 업로드됩니다 (프로젝트 디렉토리에서 볼 수 있음).하지만 내 페이지에서 변경된 사항은 없습니다.
조언을 좀 주시겠습니까? 또는 여러 파일을 업로드하고 동시에 하나의 양식 필드 만 갖는 다른 방법이 있습니까? 대단히 감사합니다.
나는이 문제를 이런 식으로 해결했습니다