Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to use ServiceManager in Model Class?

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;
}
like image 423
Emmanuel Gauthier Avatar asked Feb 14 '13 13:02

Emmanuel Gauthier


1 Answers

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.

like image 175
Andrew Avatar answered Nov 05 '22 21:11

Andrew