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 8579141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:32:57+00:00 2026-06-11T20:32:57+00:00

Following code is working fine with AbstractPlugin but I need to access entity manager

  • 0

Following code is working fine with AbstractPlugin but I need to access entity manager in view helper. How can I get entity manager in view helper?

Is there way to save entityManager instance somewhere in registry so that I can access it whereever i want? would that be a good practice?

use Zend\View\Helper\AbstractHelper;
use Doctrine\ORM\EntityManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use Zend\ServiceManager\ServiceManager;


class IsAuthz extends AbstractHelper implements ServiceManagerAwareInterface 
{

    /*
     * @var Doctrine\ORM\EntityManager
    */
    protected $em;
    protected $sm;    

    public function __construct($e) {
        $app = $e->getParam('application');
        $em = $this->getEntityManager();        
    }


    public function __invoke()
    {     
        return $this;

    }



/**
     * @return Doctrine\ORM\EntityManager
     */
    public function getEntityManager() 
    {

        if (null === $this->em) {
            $this->em = $this->sm->getServiceLocator()->get('doctrine.entitymanager.orm_default');
        }
        return $this->em;
    }

    /**
     * 
     * @param \Doctrine\ORM\EntityManager $em
     */
    public function setEntityManager(EntityManager $em) 
    {
        $this->em = $em;
    }

    /**
     * Retrieve service manager instance
     *
     * @return ServiceManager
     */
    public function getServiceManager() 
    {
        return $this->sm->getServiceLocator();
    }

    /**
     * Set service manager instance
     *
     * @param ServiceManager $locator
     * @return void
     */
    public function setServiceManager(ServiceManager $serviceManager) 
    {
        $this->sm = $serviceManager;
    }



}

ERROR:

Fatal error: Call to a member function getServiceLocator() on a non-object in XXX/XXX/src/XXX/View/Helper/IsAuthz.php on line 41
  • 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-11T20:32:58+00:00Added an answer on June 11, 2026 at 8:32 pm

    As “Daniel M” said, it is best to use the service to keep the data logic separate. but someone still want to use data logic in view helper, he can do it as below. I have changed the above code in question as follow.

    public function __construct($e, $sm) {
            $app = $e->getParam('application');
            $this->sm = $sm;
            $em = $this->getEntityManager();        
        }
    

    and then I inject the $sm object from module.php using

        public function onBootstrap($e)
        {
          $serviceManager->get('viewhelpermanager')->setFactory('IsAuthz', function ($sm) use ($e) {
               return new \xxx\View\Helper\IsAuthz($e, $sm); 
            });
    }
    

    better way is to use service in zend framework 2 for this purpose.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'using the following code to create thumbnails using ffmpeg but it was working fine
I have the following code working fine but the problem is that it always
I have the following code working fine, but I'm unable to add a zoom
The following code is working fine in other browsers, but is not on IE8
Fairly new to asp.net Mvc and jquery. Have the following code working fine, but
The following code is working fine in Android 1.5-2.2.1 but it's not in 2.3
The following code is working fine in C but when I try to write
The following code is working just fine but I have a few questions realated
I have the following code that is working fine in jsfiddle - http://jsfiddle.net/darkajax/FHZBy/ I've
I have following code And it is working fine in AIR device simulator on

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.