php - Zend Framework 2 - Showing the content of a database -
i'm making kind of market site zend framework 2. home got slider showing products (realized css3 keyframes) , text. both sliding pictures , text read mysql database. result, no output no errors. slider gets many pictures database rows, still no content echoed; plus if try change things (like db credentials or getter functions in model) throws errors expected, reads db , problem elsewhere.
db text has 3 fields: id name text
model text (home.php; there's homeinterface.php defining functions)
<?php namespace site\model; class home implements homeinterface { protected $id; protected $name; protected $text; public function getid() { return $this->id; } public function getname() { return $this->name; } public function gettext() { return $this->text; } } ?>
mapper text
<?php namespace site\mapper; 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} non esiste."); } } ?>
mapper text has factory, since has dependencies:
<?php namespace site\factory; use site\mapper\textmapper; use zend\servicemanager\factoryinterface; use zend\servicemanager\servicelocatorinterface; use site\model\home; use zend\stdlib\hydrator\classmethods; class textmapperfactory implements factoryinterface { public function createservice(servicelocatorinterface $servicelocator) { return new textmapper($servicelocator->get("zend\db\adapter\adapter"), new home(), new classmethods(false)); } } ?>
service text:
<?php namespace site\service; 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); } } ?>
factory service:
<?php namespace site\factory; use site\service\homeservice; use zend\servicemanager\factoryinterface; use zend\servicemanager\servicelocatorinterface; class homeservicefactory implements factoryinterface { public function createservice(servicelocatorinterface $servicelocator) { $textmapper = $servicelocator->get("site\mapper\textmapperinterface"); return new homeservice($textmapper); } } ?>
controller
<?php namespace site\controller; use zend\mvc\controller\abstractactioncontroller; use site\service\homeserviceinterface; use zend\view\model\viewmodel; class skeletoncontroller extends abstractactioncontroller { protected $homeservice; public function __construct(homeserviceinterface $homeservice) { $this->homeservice = $homeservice; } public function indexaction() { return new viewmodel(array ( "home" => $this->homeservice->findtext("home") )); } } ?>
finally, view:
<?php echo $this->home->gettext(); ?>
code slider similar , both parts of page having same problem. said, db detected, tables , columns too, aren't empty nothing gets echoed. interfaces written, defining functions. views in site\view\site\skeleton folder. clues problem is? thank you.
your code looks good. issue can see using classmethods
hydrator , have no setters on entity.
the hydrator will use entity api hydrate entity, if setid
, setname
or settext
not callable values not set.
although recommend adding missing methods can use zend\stdlib\hydrator\reflection
set entity properties without setters (via spl reflectionproperty
)
Comments
Post a Comment