Reputation: 2885
It seems that there is no documentation about how to delete images programmatically with Sonata Media Bundle.
I found another post saying that you need to call delete
on the media manager.
Here is my mapping:
/**
* @var Application\Sonata\MediaBundle\Entity\Media
*
* @ORM\ManyToOne(targetEntity="Application\Sonata\MediaBundle\Entity\Media", cascade={"persist", "remove"}, fetch="LAZY")
* @ORM\JoinColumn(name="image_id", referencedColumnName="id", onDelete="SET NULL")
*/
protected $image;
And this is the code I use to delete:
$this->mediaManager = $this->getContainer()->get('sonata.media.manager.media');
$previousMedia = $myEntity->getImage();
$provider = $this->getContainer()->get($previousMedia->getProviderName());
$provider->removeThumbnails($previousMedia);
$this->mediaManager->delete($previousMedia);
The association is correctly remove (the entity image
field is now null
) but on my gallery the object still exists and looks like this:
What am I doing wrong?
Upvotes: 4
Views: 638