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

  • Home
  • SEARCH
  • 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 4575708
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:06:10+00:00 2026-05-21T20:06:10+00:00

I am integrating Zend Framework and Doctrine 2. The question is, in my controllers

  • 0

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 EntityManager a Singleton already ? -> (update) not it is not
  • 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-05-21T20:06:11+00:00Added an answer on May 21, 2026 at 8:06 pm

    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

    <?php
    
    namespace My\Controller\Action\Helper;
    
    use Doctrine\ORM\EntityManager;
    
    class Service extends \Zend_Controller_Action_Helper_Abstract
    {
    
        private $em;
    
        public function __construct(EntityManager $em)
        {
            $this->em = $em;
        }
    
        public function direct($serviceClass)
        {
            return new $serviceClass($this->em);
        }
    
    }
    

    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):

    <?php
    
    use Zend_Controller_Action_HelperBroker as HelperBroker,
        My\Controller\Action\Helper\Service;
    
    class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
    {
    
        public function _initActionHelpers()
        {
            $this->bootstrap('doctrine');
            $em = $this->getResource('doctrine');
    
            HelperBroker::addHelper(new Service($em));
        }
    
    }
    

    Now write a simple Service.

    My/Domain/Blog/Service/PostService.php

    <?php
    
    namespace My\Domain\Blog\Service;
    
    use Doctrine\ORM\EntityManager,
        My\Domain\Blog\Entity\Post;
    
    class PostService implements \My\Domain\Service
    {
    
        private $em;
    
        public function __construct(EntityManager $em)
        {
            $this->em = $em;
        }
    
        public function createPost($data)
        {
            $post = new Post();
            $post->setTitle($data['title']);
            $post->setContent($data['content']);
    
            $this->em->persist($post);
            $this->em->flush(); // flush now so we can get Post ID
    
            return $post;
        }
    
    }
    

    And to bring it all together in a controller action:

    <?php
    
    class Blog_PostController extends Zend_Controller_Action
    {
    
        private $postService;
    
        public function init()
        {
            $this->postService = $this->_helper->Service('My\Domain\Blog\PostService');
        }
    
        public function createPostAction()
        {
            // normally we'd get data from the actual request
            $data = array(
                "title" => "StackOverflow is great!",
                "content" => "Imagine where I'd be without SO :)"
            );
            // and then validate it too!!
    
            $post = $this->postService->createPost($data);
            echo $post->getId(); // Blog post should be persisted
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm integrating doctrine with Zend Framework. I've hit an error thrown from cli. It
I am integrating Zend Framework and Doctrine 2 , and I am discovering the
Though this thread recommends using it, I've few concerns: I started with Zend Framework
I am integrating Zend Framework into an existing application. I want to switch the
I'm using Zend Framework and I feel that I need a good ORM to
I am integrating tinyMCE editor along with a system am developing with Zend Framework.
I'm building a paid membership site in php using the Zend Framework. I need
I'm integrating my application so that it can edit files stored in SharePoint. I'm
I'm currently learning Zend Framework and now I am looking for a way to
I was looking at Zend_Paginator in Zend Framework project using MVC, and it looks

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.