Last active
May 31, 2016 15:41
-
-
Save lemoinem/5497d46df74ac9a9df14 to your computer and use it in GitHub Desktop.
Workaround https://github.com/sonata-project/SonataClassificationBundle/issues/60 for SonataMedia < 3.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Listener; | |
use JMS\DiExtraBundle\Annotation\InjectParams; | |
use JMS\DiExtraBundle\Annotation\Inject; | |
use JMS\DiExtraBundle\Annotation\Service; | |
use JMS\DiExtraBundle\Annotation\Tag; | |
use Sonata\MediaBundle\Listener\ORM\MediaEventSubscriber; | |
/** | |
* Workaround for https://github.com/sonata-project/SonataClassificationBundle/issues/60 for SonataMedia < 3.0 | |
* | |
* @Service | |
* @Tag("doctrine.event_listener", attributes = {"event" = "onClear"}) | |
*/ | |
class OnClearListener | |
{ | |
/** | |
* @var MediaEventSubscriber | |
*/ | |
protected $mediaBundleSubscriber; | |
/** | |
* @param MediaEventSubscriber $mediaEventSubscriber | |
* | |
* @InjectParams({ | |
* "mediaEventSubscriber" = @Inject("sonata.media.doctrine.event_subscriber") | |
* }) | |
*/ | |
public function __construct(MediaEventSubscriber $mediaEventSubscriber) | |
{ | |
$this->mediaBundleSubscriber = $mediaEventSubscriber; | |
} | |
public function onClear() | |
{ | |
$rootCategories = new \ReflectionProperty(MediaEventSubscriber::class, 'rootCategories'); | |
$rootCategories->setAccessible(true); | |
$rootCategories->setValue($this->mediaBundleSubscriber, null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This fix has been merged with sonata-project/SonataMediaBundle#1017 and should therefore be included in the next release. I will leave the fix here in case you need it for a previous version. If you don't need Classification Bundle, keep an eye open for sonata-project/SonataMediaBundle#794 too.
This a drop-in, ready-to-use (granted you're using JMSDiExtraBundle) Workaround for sonata-project/SonataClassificationBundle#60
Just download the file as
src/AppBundle/Listener/OnClearListener.php
. The following command, from the root of your Symfony2 app would take care of it (You can just copy-paste it all, it will take care of creating the folder, downloading the file and committing it):Since the fix will only be available for Sonata Media 3.x, I thought even a hackish workaround would be better than no workaround at all for the previous versions.
For the record, the bug is in the Sonata Media Bundle / Sonata Classification Bundle bridge and occurs when trying to persist a
Media
after theEntityManager
has beenclear()
ed. The symptomatic error message is: