ZF2 and EntityManager (Doctrine)

I have a problem. I am trying to get Entity-Manager without a controller, but I have not found a path. At this time, I get Entity-Manager as follows:

(Controller) public function getEntityManager() { if (null === $this->_em) { $this->_em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager'); } return $this->_em; } (Plugin) public function getEntityManager() { if($this->_em == null){ $this->_em = $this->getController()->getServiceLocator()->get('doctrine.entitymanager.orm_default'); } return $this->_em; } 

You see, I always need a controller. But, if I need an EntityManager in the model, I have a problem. I can give the model to the controller, but I think this is a really bad way.

Do you have an idea to get EntityManager without a controller?

+6
source share
2 answers

The way I handle Doctrine through Services, I do it like this:

 //some Controller public function someAction() { $service = $this->getServiceLocator()->get('my_entity_service'); return new ViewModel(array( 'entities' => $service->findAll() )); } 

Service->findAll() will look something like this:

 public function findAll() { return $this->getEntityRepository()->findAll(); } 

Now we need to define my_entity_service . I am doing this inside my Module.php

 public function getServiceConfig() { return array( 'factories' => array( 'my_entity_service' => 'Namespace\Factory\MyServiceFactory' ) ); } 

Next, I create Factory (note: this can also be done through an anonymous function inside the .php module)

 <?php namespace Namespace\Factory; use Zend\ServiceManager\ServiceLocatorInterface; use Zend\ServiceManager\FactoryInterface; use Namespace\Model\MyModel; class MyServiceFactory implements FactoryInterface { /** * Create service * * @param ServiceLocatorInterface $serviceLocator * @return mixed */ public function createService(ServiceLocatorInterface $serviceLocator) { $myModel= new MyModel(); $myModel->setEntityManager($serviceLocator->get('Doctrine\ORM\EntityManager')); return $myModel; } } 

Now this is a lot to chew: D I fully understand this. What is happening here is actually very simple. Instead of creating your own model and somehow getting into the EntityManager, you call the ZF2 ServiceManager to create the Model for you and insert the EntityManager into it.

If this still bothers you, I will be happy to try to explain myself better. However, for further clarification, I would like to know about your use case. Ie: Why do you need EntityManager or where exactly do you need it.

This sample code is outside the scope of the questions.

Just to give you a live example of what I do through ServiceFactories with forms:

 public function createService(ServiceLocatorInterface $serviceLocator) { $form = new ReferenzwertForm(); $form->setHydrator(new DoctrineEntity($serviceLocator->get('Doctrine\ORM\EntityManager'))) ->setObject(new Referenzwert()) ->setInputFilter(new ReferenzwertFilter()) ->setAttribute('method', 'post'); return $form; } 
+7
source

Your real question is: " How to get an instance of ServiceManager in my own classes "

To do this, take a look at the document: (at the bottom of the page http://zf2.readthedocs.org/en/latest/modules/zend.service-manager.quick-start.html )

By default, the Zend Framework MVC registers an initializer that will enter an instance of ServiceManager, which is an implementation of Zend \ ServiceManager \ ServiceLocatorInterface, in any class implementation of Zend \ ServiceManager \ ServiceLocatorAwareInterface. A simple implementation is as follows.

to inject ServiceLocatorInterface into your classes, and then inside your class you can call:

 $this->getServiceLocator()->get('doctrine.entitymanager.orm_default'); 

or any other service you have registered.

+4
source

All Articles