文件“”不存在(VichUploaderBundle)

The file "" does not exist (VichUploaderBundle)

我的问题是,当我尝试结合使用 VichUploadBundler 和 sonata-admin 上传 mp3 文件时,上传文件时出现以下错误。

"The file "" was not found"

我的项目中有另一种形式,我上传了一些图像并且我没有问题,所以我很震惊,因为我认为音频文件会更容易

这是我的实体文件

/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* @Vich\UploadableField(mapping="episode_audio", fileNameProperty="audioName")
*
* @var File
*/
private $audioFile;

/**
* @ORM\Column(type="string", length=255)
*
* @var string
*/
private $audioName;

/**
* @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 $audioFile
*/
public function setAudioFile(?File $audioFile = null): void
{
   $this->audioFile = $audioFile;
   if (null !== $audioFile) {
       // 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 getAudioFile(): ?File
{
   return $this->audioFile;
}

public function setAudioName(?string $audioName): void
{
    $this->audioName = $audioName;
}

public function getAudioName(): ?string
{
    return $this->audioName;
}

我的 vich_uploader.yaml 文件

vich_uploader:
    db_driver: orm

    mappings:
        ..//

        episode_audio:
            uri_prefix: /audio
            upload_destination: '%kernel.project_dir%/public/audio/'
            delete_on_update: true
            delete_on_remove: true
            inject_on_load: true

最后是我的管理员 class

    protected function configureFormFields(FormMapper $formMapper) {
    $formMapper
      ->add('audioName')
      ->add('audioFile', VichFileType::class, [
        'label' => 'Audio'
      ]);
    }

我只是想在我的文件夹中看到我的音频文件,就像图像一样,我很确定这是愚蠢的,如果你们需要在我的代码中看到其他东西,请问我可以更新问题

我不得不改变一些东西来解决我的问题

首先,我在名为“$audioSize”的实体中添加了一个新的 属性,如下所示:

 /**
 * @ORM\Column(type="integer")
 *
 * @var integer
 */
private $audioSize;

//getters and setters ...

我也必须更改音频文件 属性,就像这样...

/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* @Assert\File(
*     maxSize="1024M",
*     mimeTypes={"audio/mpeg", "audio/mp3"}
* )
* @Vich\UploadableField(mapping="episode_audio", fileNameProperty="audioName", size="audioSize")
*
* @var File $audioFile
*/
private $audioFile;

最后在项目 php.ini 文件中我不得不更改文件的上传最大大小

post_max_size = 1024M
upload_max_filesize = 1024M

我会把它留在这里以备将来参考,以防其他人遇到同样的问题。

p.d 特别感谢@Iaviku,他让我想到了添加那些最终解决了我的问题的字段。