I am integrating Zend Framework and Doctrine 2.
The question is, in my controllers and view, in need to access the model. I can do all this through a single instance of the EntityManager.
Where do I store this instance ?
Zend_Registry? That’s where it is now, it is accessible from everywhere, but not really practical :$em = Zend_Registry::get('EntityManager');- As a controller and view property ? That would be accessible as
$this->em, I like this - Create a factory class that will return the instance ?
$em = My\EntityManager\Factory::getInstance();. Encapsulation is good, but long to type… - Is the
EntityManagera Singleton already ? -> (update) not it is not
I wouldn’t recommend using the EntityManager directly in your Controllers and Views. Instead, use a Service layer and inject the EntityManager it that.
I have two custom action helpers, one to retrieve Repositories and one for Services. Each action hold a reference to the EntityManager and inject it accordingly before handing it back to the Controller.
Not my actual code but something like this (not tested):
My/Controller/Action/Helper/Service.php
You can write a similar Action Helper to retrieve Repositories.
Then, register the helper in your bootstrap (where we also have access to the EntityManager):
Now write a simple Service.
My/Domain/Blog/Service/PostService.php
And to bring it all together in a controller action: