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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:30:10+00:00 2026-06-18T22:30:10+00:00

I’m trying to use Service Manager on my entity class but I don’t know

  • 0

I’m trying to use Service Manager on my entity class but I don’t know the best way to do that.

It’s easy on a controller because we can call service manager with : $this->getServiceLocator();

But, in my entity class, even if I implements ServiceLocatorAwareInterface i can retieve ServiceManager because my entity class isn’t call with the service manager :

So what is the best way :

1 – Pass serviceManager in my entity class from my controller
2 – Using ServiceManager to build my entity class
3 – … ?

To best understand my problem, that’s my code which doesn’t work :

My entity class:

class Demande extends ArraySerializable implements InputFilterAwareInterface {
/../
    public function getUserTable() {
    if (! $this->userTable) {

        $sm = $this->getServiceLocator();//<== doesn't work !
        $this->userTable = $sm->get ( 'Application\Model\UserTable' );
    }
    return $this->userTable;
}
  • 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-18T22:30:12+00:00Added an answer on June 18, 2026 at 10:30 pm

    I wouldn’t inject the ServiceManager into your model (although you can). I would rather get the ServiceManager to build your Model for you, and inject anything you need directly into the model.

    Service Config:

    'factories' => array(
        'SomethingHere' => function($sm) {
            $model= new \My\Model\Something();
    
            return $model;
        },
        '\My\Model\Demande' => function($sm) {
            $model= new \My\Model\Demande();
            /**
             * Here you use the SM to inject any dependencies you need
             * into your model / service what ever..
             */
            $model->setSomething($sm->get('SomethingHere'));
    
            return $model;
        },
        /**
         * Alternatively you can provide a class implementing
         * Zend\ServiceManager\FactoryInterface
         * which will provide an instance for you instad of using closures
         */
        '\My\Model\DemandeDefault' => '\My\Model\DemandeFactory',
    

    Place any of your dependencies inside the Service Manager Config, and then use that to inject any dependencies into your models, services etc for you.

    An example factory class if you want to use the factory method rather than closures:

    DemandeFactory.php

    use Zend\ServiceManager\FactoryInterface;
    use Zend\ServiceManager\ServiceLocatorInterface;
    
    class DemandeFactory implements FactoryInterface
    {
        /**
         * Create a new Instance
         *
         * @param ServiceLocatorInterface $serviceLocator
         * @return Demande
         */
        public function createService(ServiceLocatorInterface $serviceLocator)
        {
            $config = $serviceLocator->get('Config'); // if you need the config..
            // inject dependencies via contrustor
            $model = new \My\Model\Demande($serviceLocator->get('SomethingHere'));
            // or using setter if you wish.
            //$model->setSomething($serviceLocator->get('SomethingHere'));
    
            return $model;
        }
    }
    

    An example Model you are trying to instantiate via the Service Manager.

    Demande.php

    class Demande
    {
        protected $_something;
    
        /**
         * You can optionally inject your dependancies via your constructor
         */
        public function __construct($something)
        {
            $this->setSomething($something);
        }
    
        /**
         * Inject your dependencies via Setters
         */
        public function setSomething($something)
        {
            $this->_something = $something;
        }
    
        // Something will be injected for you by the Service Manager
        // so there's no need to inject the SM itself.
    }
    

    In your Controller:

    public function getDemande() 
    {
        if (! $this->_demande) {
            $sm = $this->getServiceLocator();
            $this->_demande = $sm->get ('\My\Model\Demande');
        }
        return $this->_demande;  
    }  
    

    You could inject the SergiceManager/ServiceLocator into your models but then your models will depend on the ServiceLocator.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.

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.