Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8760641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:05:57+00:00 2026-06-13T15:05:57+00:00

I have a problem. I try to get the Entity-Manager without a Controller, but

  • 0

I have a problem. I try to get the Entity-Manager without a Controller, but I found no way.
At this time, I get the Entity-Manager like this:

(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 need allways a controller. But, if I need the EntityManager in a model, i have a problem. I can give the model the controller, but I think this is really a bad way.

Have you any idea to get the EntityManager without a controller?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:05:58+00:00Added an answer on June 13, 2026 at 3:05 pm

    The way I handle Doctrine is through Services, i do it like the following:

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

    The Service->findAll() would look something like this:

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

    Now we need to define the my_entity_service. I do this inside my Module.php

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

    Next I create the Factory (note: this could also be done via anonymous function inside the Module.php)

    <?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 😀 I totally get that. What is happening here is actually very simple though. Instead of creating your model and somehow get to the EntityManager, you call ZF2’s ServiceManager to create the Model for you and inject the EntityManager into it.

    If this is still confusing to you, I’ll gladly try to explain myself better. For further clarification however I’d like to know about your use case. I.e.: what for do you need the EntityManager or where exactly do u need it.

    This code example is outside of the question scope

    Just to give you a live example of something I do via 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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an entity like this: @Entity @XmlRootElement public class Article { @Id @GeneratedValue(strategy
i have a problem when try to get and parse a remote JSON. require
I have this problem when i try to synchronize a observable list with listbox/view
i am having this problem try to use the cocos2d CCScrollLayer I have added
I have a problem with JPA 1.0 (OpenJPA) Following situation @Entity public class A{
I have problem in a JPA entity class. This entity was working perfectly as
I have a problem when try to use hashtable with large data. I have
I have a problem when I try to count how many times a word
I have a problem when I try to persist my model. An exception is
I have a problem when I try to set up Xcode . shown in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.