Skip to content

Instantly share code, notes, and snippets.

@lemoinem
Last active May 31, 2016 15:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lemoinem/5497d46df74ac9a9df14 to your computer and use it in GitHub Desktop.
Save lemoinem/5497d46df74ac9a9df14 to your computer and use it in GitHub Desktop.
<?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);
}
}
@lemoinem
Copy link
Author

lemoinem commented Dec 23, 2015

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):

[ ! -d "src/AppBundle/Listener" ] && mkdir "src/AppBundle/Listener" ;\
wget -O "src/AppBundle/Listener/OnClearListener.php" 'https://gist.githubusercontent.com/lemoinem/5497d46df74ac9a9df14/raw/87df3432748fc0d8ea4d96e0662f621397633f59/OnClearListener.php' ;\
git add 'src/AppBundle/Listener/OnClearListener.php' ;\
git commit -o 'src/AppBundle/Listener/OnClearListener.php' -m 'Workaround sonata-project/SonataClassificationBundle#60 for SonataMediaBundle < 3.0'

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 the EntityManager has been clear()ed. The symptomatic error message is:

A managed+dirty entity default can not be scheduled for insertion. :
[...]
'INSERT INTO classification__context (name, enabled, created_at, updated_at,
id) VALUES (?, ?, ?, ?, ?)' with params [...]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment