2

I am using zf-boilerplate for my zend framework project. I am using the Gedmo extensions and need to get an instance of the doctrine event manager in the bootstrap.

ie. I would like to be able to do the following:

$evm = $this->getDoctrine()->getEventManager()

How can I do this?

akond
  • 15,865
  • 4
  • 35
  • 55
dimbo
  • 817
  • 1
  • 11
  • 25
  • `EventManager` or `EntityManager`? Not trying to be a pedantic PITA (really!); just want to be sure. – David Weinraub Sep 23 '12 at 16:22
  • It is the event manager that I need in order to hook into Gedmo translatable and set the locale to be used during the bootstrap – dimbo Sep 24 '12 at 09:35
  • Hmmm, but the accepted answer refers to `EntityManager`? – David Weinraub Sep 24 '12 at 12:14
  • Ah, you're right, but getting the event manager from the entity manager is fairly trivial per my original question. Thanks for looking though. – dimbo Sep 24 '12 at 14:35

1 Answers1

1

You can access the entity manager by calling

Zend_Registry::get('em');

If you're in the middle of bootsrap, then make sure you bootstrap entity manager before accessing it.

Just like resource methods, you use the bootstrap() method to execute resource plugins. Just like with resource methods, you can specify either a single resource plugin, multiple plugins (via an array), or all plugins. Additionally, you can mix and match to execute resource methods as well. (Zend Framework manual)

i.e. execute the code below first

// Execute all resource methods and plugins:
$bootstrap->bootstrap('doctrine');

UPDATE

Didn't notice the question was about EventManager. Use the code below in order to get access to it

Zend_Registry::get('doctrine')->getEventManager ();

akond
  • 15,865
  • 4
  • 35
  • 55
  • Yes, this is what I usually use in the controllers etc. but it seems that because it is in the bootstrap this registry entry has not been set yet. I get the following error: Fatal error: Uncaught exception 'Zend_Exception' with message 'No entry is registered for key 'em'' in /var/www/myswap/library/Zend/Registry.php on line 147 – dimbo Sep 23 '12 at 12:43
  • Hi, If I execute $bootstrap->bootstrap(); first, I get the following error: Notice: Undefined variable: bootstrap – dimbo Sep 24 '12 at 09:49
  • Yes, $bootstrap in the instance of Bootstrap. If you are in bootstrap already then it should be $this instead of $bootstrap. – akond Sep 24 '12 at 09:50
  • Hi, If I do this I get the following error: Fatal error: Uncaught exception 'Zend_Application_Bootstrap_Exception' with message 'Circular resource dependency detected' in /var/www/myswap/library/Zend/Application/Bootstrap/BootstrapAbstract.php on line 662 – dimbo Sep 24 '12 at 11:10
  • Replace `$this->boostrap ()` with `$this->boostrap ('doctrine');` – akond Sep 24 '12 at 11:18
  • Thanks. That did the trick. I will mark it as answered, but I wanted to ask if executing the doctrine resource here like I have done in the bootstrap will create a considerable overhead? – dimbo Sep 24 '12 at 11:45
  • I'm not sure what you call considerable, but connection to a database sure will take some time. In my environment it is ok. YMMV. – akond Sep 24 '12 at 11:49
  • Should be no additional overhead. It's just bootstrapping Doctrine which takes place anyway. You are just making sure that the Doctrine bootstrapping takes before you do your other bootstrapping. And remember, bootstrapping the resource does not necessarily imply connecting to the underlying database. Doctrine (almost certainly) lazy-connects to the datasource. – David Weinraub Sep 24 '12 at 12:16