2015-07-09 5 views
-3

Я использую https://github.com/akrabat/zf2-tutorial/tree/master/module/Album, и у меня есть проблема.ZF2 __construct()

Catchable fatal error: Argument 1 passed to Album\Model\AlbumTable::__construct() must be an instance of Album\Model\TableGateway, instance of Zend\Db\TableGateway\TableGateway given, called in C:\xampp\htdocs\WebAppCards\module\Album\Module.php on line 38 and defined in C:\xampp\htdocs\WebAppCards\module\Album\src\Album\Model\AlbumTable.php on line 13 

module.php

namespace Album; 

// Add these import statements: 
use Album\Model\Album; 
use Album\Model\AlbumTable; 
use Zend\Db\ResultSet\ResultSet; 
use Zend\Db\TableGateway\TableGateway; 

class Module 
{ 
    // ... 
    public function getServiceConfig() 
    { 
     return array(
      'factories' => array(
       'Album\Model\AlbumTable' => function($sm) { 
        $tableGateway = $sm->get('AlbumTableGateway'); 
        $table = new AlbumTable($tableGateway); 
        return $table; 
       }, 
       'AlbumTableGateway' => function ($sm) { 
        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); 
        $resultSetPrototype = new ResultSet(); 
        $resultSetPrototype->setArrayObjectPrototype(new Album()); 
        return new TableGateway('album', $dbAdapter, null, $resultSetPrototype); 
       }, 
      ), 
     ); 
    } 
    //... 
} 

AlbumTable.php:

namespace Album\Model; 

use Zend\Db\TableGateway\AbstractTableGateway; 
use Zend\Db\Adapter\Adapter; 
use Zend\Db\ResultSet\ResultSet; 

class AlbumTable extends AbstractTableGateway 
{ 
    protected $table = 'album'; 

    public function __construct(Adapter $adapter) 
    { 
     $this->adapter = $adapter; 
     $this->resultSetPrototype = new ResultSet(); 
     $this->resultSetPrototype->setArrayObjectPrototype(new Album()); 

     $this->initialize(); 
    } 
    //... 
} 

http://wklej.org/id/1753971/

+1

Ничего общего с ZF2, это * пространство имен * проблема. Вам не хватает декларации в «AlbumTable»; добавьте 'использовать Zend \ Db \ TableGateway \ TableGateway' в верхней части класса. – AlexP

+0

По-прежнему такая же ошибка. – webmazz

+1

* такой же * ошибка? Он не соответствует вашему коду. «Аргумент 1, переданный в« Album \ Model \ AlbumTable :: __ construct() », должен быть экземпляром **« Album \ Model \ TableGateway »** в вашем примере, который указывает тип метода на« Adapter ». – AlexP

ответ

3

В соответствии с official ZF2 tutorial, изменить тип намек на конструктор в AlbumTable.

public function __construct(TableGateway $tableGateway) 
{ 
    $this->tableGateway = $tableGateway; 
} 

Если вы действительно хотите, чтобы принять класс адаптера в конструкторе, убедитесь, что вы предоставить ему один в вашем Module.php вместо Zend\Db\TableGateway\TableGateway класса.