symfony - Sonata Media: Change context programmatically -
i'm writing little blog app user can publish public , private news. users can attach files these news. have 2 contexts app: public_news
, files can accessed everyone; , private_news
, files can accessed if user has log on.
i want able move files public_news context private_news context when user changes news public private, , vice versa.
i hoping simple $media->setcontext('private_news');
, won't move physical file 1 directory other.
what think recreating media?
$oldmedia = getyouroldmedia(); // $media = clone($oldmedia); # me didn't work expected # ymmv - didn't spend lots wondering $media = new media(); // work fine image , file provider, // not tested 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 other data you're interested in */ $mediamanager->save($media); $mediamanager->delete($oldmedia);
$mediamanager->delete
might not delete physical files depending on provider, might want create own provider if wish so.
edit:
on further research found out can manualy delete files before deleting old media:
if ($pool->getfilesystem()->has($path)) { $pool->getfilesystem()->delete($path); }
but don't before saving new media entity.
Comments
Post a Comment