您如何看待重新创建这种媒体?
$oldMedia = getYourOldMedia();
// $media = clone($oldMedia); # For me it didn't work as expected
# YMMV - I didn't spend lots wondering about that
$media = new Media();
// This will work fine with image and file provider,
// but it was not tested with other providers
$pool = $container->get('sonata.media.pool');
$provider = $pool->getProvider($oldMedia->getProviderName());
$media->setBinaryContent($provider->getReferenceFile($oldMedia));
}
$media->setProviderName($oldMedia->getProviderName());
$media->setContext('private_news');
/* copy any other data you're interested in */
$mediaManager->save($media);
$mediaManager->delete($oldMedia);
$mediaManager->delete
可能不会删除您的物理文件,具体取决于提供者,如果您愿意,您可能希望创建自己的提供者。
编辑:
在进一步研究中,我发现您可以在删除旧媒体之前手动删除文件:
if ($pool->getFilesystem()->has($path)) {
$pool->getFilesystem()->delete($path);
}
但在保存新媒体实体之前不要这样做。