且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何正确删除使用 Sonata-media-bundle 上传的所有图像

更新时间:2023-12-05 17:46:34

为了能够正确删除与上传文件相关的所有媒体,必须使用适当的提供程序:

To be able to correctly delete all media related to the uploaded file, one has to use the appropiate provider:

来自奏鸣曲媒体文档

提供者类负责处理与媒体资产相关的常见事情:

A provider class is responsible for handling common things related to a media asset:

  • 缩略图
  • 路径
  • 使用表单编辑媒体
  • 存储媒体信息(元数据)

所以代码看起来像:

$media = $mediaManager->findOneBy(array('id' => $id));
//assuming you have access through $this->get to the service container
$provider = $this->get($media->getProviderName());
$provider->removeThumbnails($media);
$mediaManager->delete($media)