Hiranya February 2016

Error:must implement interface Zend\Stdlib\Hydrator\HydratorInterface

I am following https://zf2-docs.readthedocs.org/en/latest/in-depth-guide/zend-db-sql-zend-stdlib-hydrator.html

Here is my 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',n        

Answers


Wilt February 2016

You forgot a bracket when constructing your ZendDbSqlMapper instance, that is why the second and third parameter aren't passed.

Here you go wrong:

return new ZendDbSqlMapper($serviceLocator->get('Zend\Db\Adapter\Adapter',new ClassMethods(false),new Post()));
                                                                         <-- ) missing                       <-- ) too many

Try like this:

return new ZendDbSqlMapper(
    $serviceLocator->get('Zend\Db\Adapter\Adapter'),
    new ClassMethods(false),
    new Post()
);

Post Status

Asked in February 2016
Viewed 3,055 times
Voted 9
Answered 1 times

Search




Leave an answer