php - Error:must implement interface Zend\Stdlib\Hydrator\HydratorInterface -
i following https://zf2-docs.readthedocs.org/en/latest/in-depth-guide/zend-db-sql-zend-stdlib-hydrator.html
here code
zenddbsqlmapper.php
namespace blog\mapper; use blog\model\postinterface; use zend\db\adapter\adapterinterface; use zend\db\adapter\driver\resultinterface; use zend\db\resultset\resultset; use zend\db\resultset\hydratingresultset; use zend\db\sql\sql; use zend\stdlib\hydrator\hydratorinterface; class zenddbsqlmapper implements postmapperinterface{ protected $dbadapter; protected $hydrator; protected $postprototype; public function __construct(adapterinterface $dbadapter,hydratorinterface $hydrator,postinterface $postprototype) { $this->dbadapter = $dbadapter; $this->hydrator = $hydrator; $this->postprototype = $postprototype; } public function find($id){ } public function findall() { $sql = new sql($this->dbadapter); $select = $sql->select('posts'); $stmt = $sql->preparestatementforsqlobject($select); $result = $stmt->execute(); if($result instanceof resultinterface && $result->isqueryresult()) { $resultset = new hydratingresultset($this->hydrator,$this->postprototype); return $resultset->initialize($result); } return array(); } }
zenddbsqlmapperfactory.php
namespace blog\factory; use blog\mapper\zenddbsqlmapper; use blog\model\post; use zend\servicemanager\factoryinterface; use zend\servicemanager\servicelocatorinterface; use zend\stdlib\hydrator\classmethods; class zenddbsqlmapperfactory implements factoryinterface{ public function createservice(servicelocatorinterface $servicelocator) { return new zenddbsqlmapper($servicelocator->get('zend\db\adapter\adapter',new classmethods(false),new post())); } }
module.config.php
'service_manager'=>array( 'factories'=>array( 'blog\mapper\postmapperinterface'=>'blog\factory\zenddbsqlmapperfactory', 'blog\service\postserviceinterface'=>'blog\factory\postservicefactory', 'zend\db\adapter\adapter'=>'zend\db\adapter\adapterservicefactory' ) ),
problem:
it showing error like
catchable fatal error: argument 2 passed blog\mapper\zenddbsqlmapper::__construct() must implement interface zend\stdlib\hydrator\hydratorinterface, none given, called in /var/www/zend2/module/blog/src/blog/factory/zenddbsqlmapperfactory.php on line 16 , defined in /var/www/zend2/module/blog/src/blog/mapper/zenddbsqlmapper.php on line 21
i not understand why not detecting hydratorinterface
.is deprecated https://github.com/zendframework/zend-stdlib/issues/25
i using zend framework 2.3.3
you forgot bracket when constructing zenddbsqlmapper
instance, why second , third parameter aren't passed.
here go wrong:
return new zenddbsqlmapper($servicelocator->get('zend\db\adapter\adapter',new classmethods(false),new post())); <-- ) missing <-- ) many
try this:
return new zenddbsqlmapper( $servicelocator->get('zend\db\adapter\adapter'), new classmethods(false), new post() );
Comments
Post a Comment