Tuesday, 15 July 2014

zend framework2 - Best way to use ServiceManager in Model Class? -



zend framework2 - Best way to use ServiceManager in Model Class? -

i'm trying utilize service manager on entity class don't know best way that.

it's easy on controller because can phone call service manager : $this->getservicelocator();

but, in entity class, if implements servicelocatorawareinterface can retieve servicemanager because entity class isn't phone call service manager :

so best way :

1 - pass servicemanager in entity class controller 2 - using servicemanager build entity class 3 - ... ?

to best understand problem, that's code 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' ); } homecoming $this->usertable; }

i wouldn't inject servicemanager model (although can). rather servicemanager build model you, , inject need straight model.

service config:

'factories' => array( 'somethinghere' => function($sm) { $model= new \my\model\something(); homecoming $model; }, '\my\model\demande' => function($sm) { $model= new \my\model\demande(); /** * here utilize sm inject dependencies need * model / service ever.. */ $model->setsomething($sm->get('somethinghere')); homecoming $model; }, /** * alternatively can provide class implementing * zend\servicemanager\factoryinterface * provide instance instad of using closures */ '\my\model\demandedefault' => '\my\model\demandefactory',

place of dependencies within service manager config, , utilize inject dependencies models, services etc you.

an illustration mill class if want utilize mill method rather closures:

demandefactory.php

use zend\servicemanager\factoryinterface; utilize zend\servicemanager\servicelocatorinterface; class demandefactory implements factoryinterface { /** * create new instance * * @param servicelocatorinterface $servicelocator * @return demande */ public function createservice(servicelocatorinterface $servicelocator) { $config = $servicelocator->get('config'); // if need config.. // inject dependencies via contrustor $model = new \my\model\demande($servicelocator->get('somethinghere')); // or using setter if wish. //$model->setsomething($servicelocator->get('somethinghere')); homecoming $model; } }

an illustration model trying instantiate via service manager.

demande.php

class demande { protected $_something; /** * can optionally inject dependancies via constructor */ public function __construct($something) { $this->setsomething($something); } /** * inject dependencies via setters */ public function setsomething($something) { $this->_something = $something; } // injected service manager // there's no need inject sm itself. }

in controller:

public function getdemande() { if (! $this->_demande) { $sm = $this->getservicelocator(); $this->_demande = $sm->get ('\my\model\demande'); } homecoming $this->_demande; }

you inject sergicemanager/servicelocator models models depend on servicelocator.

zend-framework2

No comments:

Post a Comment