如何正确删除使用 Sonata-media-bundle 上传的所有图像 [英] How to correctly delete all images uploaded using sonata-media-bundle

查看:20
本文介绍了如何正确删除使用 Sonata-media-bundle 上传的所有图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经按照建议使用 Sonata-media-bundle 上传了一些图片 这里

I have uploaded some images using the sonata-media-bundle, as suggested here

$media = new Media;
$media->setBinaryContent($file->getRealPath());
$media->setContext('myContext'); 
$media->setProviderName('sonata.media.provider.image');
$mediaManager->save($media);

以前的代码正确创建媒体所有文件(根据上下文配置)和数据库注册表.现在我为删除媒体所做的就是这样调用:

Previous code creates the media correctly all files (according to context config) and db registry. Now what I did to delete the media is calling like this:

$media = $mediaManager->findOneBy(array('id' => $id));
$mediaManager->delete($media)

它正确地从数据库中删除了媒体数据,但没有从配置的文件系统中删除,仍然有一些与上传的媒体相关联的图像.我计划使用 S3 文件系统,所以我需要使用相同的框架删除所有

It correctly deletes the media data from database but not from the configured fileSystem, there are still some images associated to the uploaded media. I'm planning using the S3 filesystem so I need to delete all with the same framework

我最相关的奏鸣曲配置(也使用建议的)是这样的:

the most relevant of my sonata configuration (using the suggested too) is like this:

<代码>奏鸣曲媒体:default_context: 默认db_driver:学说_orm班级:媒体:MyApp\MediaBundle\Entity\Media画廊:MyApp\MediaBundle\Entity\Gallerygallery_has_media: MyApp\MediaBundle\Entity\GalleryHasMedia上下文:默认:供应商:- 奏鸣曲.media.provider.youtube- 奏鸣曲.media.provider.image- 奏鸣曲.media.provider.file格式:小:{ 宽度:200,质量:77}我的上下文:供应商:- 奏鸣曲.media.provider.image格式:正常:{ 宽度:350,质量:75}tmb:{ 宽度:100,质量:75}文件系统:当地的:目录:%kernel.root_dir%/../web/uploads/media创建:假

提前致谢.

推荐答案

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

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)

这篇关于如何正确删除使用 Sonata-media-bundle 上传的所有图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆