错误:必须实现接口Zend \ Stdlib \ Hydrator \ HydratorInterface

时间:2016-02-08 08:28:44

标签: php zend-framework2

我正在关注https://zf2-docs.readthedocs.org/en/latest/in-depth-guide/zend-db-sql-zend-stdlib-hydrator.html

这是我的代码

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'
    )
),

问题:

显示错误,如

  

可捕获的致命错误:参数2传递给   Blog \ Mapper \ ZendDbSqlMapper :: __ construct()必须实现接口   Zend \ Stdlib \ Hydrator \ HydratorInterface,没有给出,调用   /var/www/zend2/module/Blog/src/Blog/Factory/ZendDbSqlMapperFactory.php   在第16行并在中定义   /var/www/zend2/module/Blog/src/Blog/Mapper/ZendDbSqlMapper.php上线   21

我不明白为什么它没有检测到HydratorInterface。它是否被弃用为https://github.com/zendframework/zend-stdlib/issues/25

我正在使用Zend framework 2.3.3

1 个答案:

答案 0 :(得分:0)

在构建ZendDbSqlMapper实例时忘记了一个括号,这就是为什么没有传递第二个和第三个参数。

这里你出错:

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

试试这样:

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