我正在集成VichUploaderBundle,并且已经能够成功提交文件并保存,下载并编辑表单,然后重新提交另一个文件并替换原始文件。
但是当我提交表单不附加任何文件时,会出现以下错误:
Expected argument of type "Symfony\Component\HttpFoundation\File\File", "NULL" given at property path "attachmentFile".
$form->handleRequest($request);
这是我以以下形式添加组件的方式:
$builder->add('attachmentFile', VichFileType::class, [
'label' => 'Upload file',
'allow_delete' => false,
'required' => false
]);
这是我的实体:
/**
* @ORM\Entity(repositoryClass="App\Repository\Campus\Material\DocumentRepository")
* @ORM\Table(name="campus_publication_document")
* @Vich\Uploadable()
*/
class Document extends Publication
{
/**
* @ORM\Column(type="string", length=255)
* @var string
*/
private $attachmentName;
/**
* @var int
* @ORM\Column(type="integer")
*/
private $attachmentSize;
/**
* @ORM\Column(type="string", length=45)
* @var string
*/
private $attachmentMime;
/**
* @ORM\Column(type="string", length=255)
* @var string
*/
private $attachmentOriginalName;
/**
* @Vich\UploadableField(
* mapping="tw_campus_document",
* fileNameProperty="attachmentName",
* size="attachmentSize",
* mimeType="attachmentMime",
* originalName="attachmentOriginalName"
* )
* @var File
*/
private $attachmentFile;
/**
* @ORM\Column(type="datetime")
* @var \DateTime
*/
private $attachmentUploadedAt;
/**
* @return string
*/
public function getAttachmentName(): ?string
{
return $this->attachmentName;
}
/**
* @param string $attachmentName
*/
public function setAttachmentName(string $attachmentName = null): void
{
$this->attachmentName = $attachmentName;
}
/**
* @return int
*/
public function getAttachmentSize(): int
{
return $this->attachmentSize;
}
/**
* @param int $attachmentSize
*/
public function setAttachmentSize(int $attachmentSize = null): void
{
$this->attachmentSize = $attachmentSize ?: 0;
}
/**
* @return string
*/
public function getAttachmentMime(): string
{
return $this->attachmentMime;
}
/**
* @param string $attachmentMime
*/
public function setAttachmentMime(string $attachmentMime = null): void
{
$this->attachmentMime = $attachmentMime;
}
/**
* @return string
*/
public function getAttachmentOriginalName(): string
{
return $this->attachmentOriginalName;
}
/**
* @param string $attachmentOriginalName
*/
public function setAttachmentOriginalName(string $attachmentOriginalName = null): void
{
$this->attachmentOriginalName = $attachmentOriginalName;
}
/**
* @return File
*/
public function getAttachmentFile(): ?File
{
return $this->attachmentFile;
}
/**
* @param File $attachmentFile
* @throws \Exception
*/
public function setAttachmentFile(File $attachmentFile): void
{
$this->attachmentFile = $attachmentFile;
if ($attachmentFile){
$this->attachmentUploadedAt = new \DateTime();
}
}
/**
* @return \DateTime
*/
public function getAttachmentUploadedAt(): \DateTime
{
return $this->attachmentUploadedAt;
}
/**
* @param \DateTime $attachmentUploadedAt
*/
public function setAttachmentUploadedAt(\DateTime $attachmentUploadedAt): void
{
$this->attachmentUploadedAt = $attachmentUploadedAt;
}
}
蚂蚁映射:
vich_uploader:
db_driver: orm
mappings:
tw_campus_document:
uri_prefix: '%paths.download.tw.campus.document%'
upload_destination: '%kernel.project_dir%%paths.uploads.tw.campus.document%'
namer: vich_uploader.namer_uniqid
directory_namer: vich_uploader.namer_directory_current_date_time
inject_on_load: true
答案 0 :(得分:1)
最近有同样的问题。我解决它在我的二传手添加这样的:
sudo bash run_script.sh