Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assign event manager to entity managers (inc fix) #6

Open
alexb-uk opened this issue Mar 15, 2011 · 6 comments
Open

Assign event manager to entity managers (inc fix) #6

alexb-uk opened this issue Mar 15, 2011 · 6 comments

Comments

@alexb-uk
Copy link

Hi,

We needed to assign an event manager to an entity manager and found we needed to extend your library.

There is one extra hurdle in that the event manager object used by the entity manager and dbal connection have to be the same thing, otherwise a mismatch exception is thrown.

I have never used github to submit changes before so I thought I would create an issue with the change I made. Sorry if this not the best way.

Any questions about my changes or suggestions very welcome:


Index: Bisna/Application/Container/DoctrineContainer.php
===================================================================
--- Bisna/Application/Container/DoctrineContainer.php   (revision 10493)
+++ Bisna/Application/Container/DoctrineContainer.php   (working copy)
@@ -67,6 +67,11 @@
      */
     private $entityManagers = array();
 
+    /**
+     * @var object Doctrine EventManager.
+     */
+    private $eventManager;
+
 
     /**
      * Constructor.
@@ -362,7 +367,7 @@
         return \Doctrine\DBAL\DriverManager::getConnection(
             $config['parameters'],
             $this->startDBALConfiguration($config),
-            $this->startDBALEventManager($config)
+            $this->startEventManager($config)
         );
     }
 
@@ -390,21 +395,31 @@
     /**
      * Initialize the EventManager.
      *
-     * @param array $config DBAL Connection configuration.
+     * @param array $config DBAL / ORM Connection configuration.
      *
      * @return Doctrine\Common\EventManager
      */
-    private function startDBALEventManager(array $config = array())
+    private function startEventManager(array $config = array())
     {
-        $eventManagerClass = $config['eventManagerClass'];
-        $eventManager = new $eventManagerClass();
+        // If no event manager class defined then return null
+        if (isset($config['eventManagerClass']) === false) {
+            return null;
+        }
+
+        // Create new event manager if not previously created
+        if (isset($this->eventManager) === false) {
+            $eventManagerClass = $config['eventManagerClass'];
+            $this->eventManager = new $eventManagerClass();
+        }
 
         // Event Subscribers configuration
-        foreach ($config['eventSubscribers'] as $subscriber) {
-            $eventManager->addEventSubscriber(new $subscriber());
+        if (isset($config['eventSubscribers']) === true) {
+            foreach ($config['eventSubscribers'] as $subscriber) {
+                $this->eventManager->addEventSubscriber(new $subscriber());
+            }
         }
 
-        return $eventManager;
+        return $this->eventManager;
     }
 
     /**
@@ -473,9 +488,13 @@
      */
     private function startORMEntityManager(array $config = array())
     {
+        // Get Event Manager for Entity Manager
+        $evm = $this->startEventManager($config);
+
         return \Doctrine\ORM\EntityManager::create(
             $this->getConnection($config['connection']),
-            $this->startORMConfiguration($config)
+            $this->startORMConfiguration($config),
+            $evm
         );
     }
/code>

Thanks for your hard work we find it very useful.

Cheers

Alex

@alexb-uk
Copy link
Author

Btw example application.ini config is the same as DBAL:

e.g.

resources.doctrine.orm.entityManagers.default.eventManagerClass  = "Doctrine\Common\EventManager"
resources.doctrine.orm.entityManagers.default.eventSubscribers[] = "Gedmo\Tree\TreeListener"

@stormblow
Copy link

I have try to use the example application.ini config you gave but it seems to be not working.
I have also tried to do this :
resources.doctrine.orm.entityManagers.default.eventSubscribers[] = "Gedmo\Tree\TreeListenerrrrrrrrrrrrrr"
and no more exceptions are raised.

@alexb-uk
Copy link
Author

alexb-uk commented Jul 7, 2011

Did you apply the patch in my first comment?

What is the exception you are seeing when a real event subscriber class is defined?

@stormblow
Copy link

I applied the patch you mentionned in your first comment.
I get this following error message :
Catchable fatal error: Argument 1 passed to Gedmo\Mapping\MappedEventSubscriber::loadMetadataForObjectClass() must be an instance of Doctrine\Common\Persistence\ObjectManager, instance of Doctrine\ORM\EntityManager given, called in D:\Perso\project\web\NOLASnowball\library\Gedmo\Tree\TreeListener.php on line 195 and defined in D:\Perso\project\web\NOLASnowball\library\Gedmo\Mapping\MappedEventSubscriber.php on line 171

I'll googled it a bit more.
Thank you

@stormblow
Copy link

I did what Christophe COEVOET sais in this post (http://www.mentby.com/Group/symfony-users/doctrine-extensions-in-symfony2-beta1.html) :
The master branch of the Gedmo extension requires Doctrine ORM 2.1beta.
If you use 2.0.5 (provided with the Standard Edition) you need to use
the 2.0.x branch of the extensions.

And it seems to be working now.

@alexb-uk
Copy link
Author

alexb-uk commented Jul 7, 2011

I suspect I know where the issue could be, we are having to use an older version of Gedmo as we are still on Doctrine 2.0 and for a while now the Gedmo trunk is being developed against 2.1.

There are some significant changes by the looks, for example my TreeListener.php has 80 lines of code and the current one has 227 lines :)

I'm not entirely sure what revision of Gedmo we are on but if you are still on Doctrine 2.0 I'm happy to package up the revision we have so you can try to confirm if that's the issue or not.

EDIT:

I didn't see your comment above, great that you have it working now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants