php - Zend Framework 2 - Multiple instances of the same service -
i have service reads db, accepting parameter sql query, , need call different values of parameter, in same view. actually, results in calling last value times. example (this in controller):
public function someaction () { return new viewmodel(array ( "welcome" => $this->homeservice->findtext("welcome"), "address" => $this->homeservice->findtext("address"), "map" => $this->homeservice->findtext("map"), )); }
doing calls homeservice parameter "map" times.
i tried set in module.config.php (site\service\homeserviceinterface homeservice of course):
'service_manager' => array( 'factories' => array( 'site\service\homeserviceinterface' => 'site\factory\homeservicefactory', ), 'shared' => array ( 'site\service\homeserviceinterface' => false ), ),
since read 'shared' array should allow have multiple instances of same service, didn't work. so, besides creating service (which i'd find horrible) can't imagine how it. ideas? thank you.
edit
homeservice (the interface has needed)
use site\model\home; use site\model\homeinterface; use site\mapper\textmapperinterface; class homeservice implements homeserviceinterface { protected $textmapper; public function __construct (textmapperinterface $textmapper) { $this->textmapper = $textmapper; } public function findtext($name) { return $this->textmapper->find($name); } }
textmapper follows:
use site\model\homeinterface; use zend\db\adapter\adapterinterface; use zend\db\adapter\driver\resultinterface; use zend\stdlib\hydrator\hydratorinterface; use zend\db\resultset\hydratingresultset; use zend\db\sql\sql; use zend\stdlib\hydrator\classmethods; class textmapper implements textmapperinterface { protected $homeprototype; protected $adapter; protected $hydrator; public function __construct(adapterinterface $adapter, homeinterface $homeprototype, hydratorinterface $hydrator) { $this->adapter = $adapter; $this->homeprototype = $homeprototype; $this->hydrator = $hydrator; } public function find($name) { $sql = new sql($this->adapter); $select = $sql->select(); $select->from("mono"); $select->where(array("name = ?" => $name)); $stmt = $sql->preparestatementforsqlobject($select); $result = $stmt->execute(); if ($result instanceof resultinterface && $result->isqueryresult() && $result->getaffectedrows()) { return $this->hydrator->hydrate($result->current(), $this->homeprototype); } throw new \invalidargumentexception("{$name} doesn't exist."); } }
homeinterface model, has getter , setter methods.
well, suppose solved, i'll post idea, surely working fine. of course, textmapper gets instantiated __construct ()
once, suppose happens same way everytime it's called in same action. decided modify everything. had db 3 rows: id
, name
(which using until in find($name)
function) , text
. model looked way:
class home implements homeinterface { protected $id; protected $name; protected $text; public function getid() { return $this->id; } public function setid($id) { $this->id = $id; } public function getname() { return $this->name; } public function setname($name) { $this->name = $name; } public function gettext() { return $this->text; } public function settext ($text) { $this->text = $text; } }
now changed table: fields id
, home
, certification
, about
, welcome
, address
, map
(the last 3 being ones need in case). new home
has new getters , setters fit new way. (gethome()
, sethome($home)
, etc...) mapper's find()
doesn't need $name
argument anymore. can use getter need when i'm in view, show text want. action simply:
public function someaction () { return new viewmodel(array ( "welcome" => $this->homeservice->findtext("welcome"), )); }
and in some.phtml page:
<?php echo $this->welcome->getwelcome(); ?> <?php echo $this->welcome->getaddress(); ?> <?php echo $this->welcome->getmap(); ?>
i suppose better design before.
Comments
Post a Comment