2013-12-10 61 views
0

想創建的Zend \ DB的一個實例\ TableGateway序與數據庫進行交互,但我總是得到這個錯誤:與Zend DB TableGateway TableGateway工作

 Catchable fatal error: Argument 1 passed to Question\Model \QuestionsTable::__construct() must be an instance of Zend\Db\TableGateway\TableGateway, none given. 
     here is how am trying to implement it:... 
     namespace Question\Model; 
      use Zend\Db\TableGateway\TableGateway; 

      class QuestionsTable 
        { 
       public $usr_id; 
      public $title; 
      public $description; 
       public $status; 

       protected $tableGateway; 

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

我猜想有一個與我如何在服務管理器實現它的錯誤:

     return array(
        'factories' => array(
          'Question\Model\QuestionsTable' => function($sm) { 
       $tableGateway = $sm->get('QuestionsTableGateway'); 
         $table = new QuestionsTable($tableGateway); 
         return $table; 
         }, 
      'QuestionsTableGateway' => function ($sm) { 
       $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); 
       $resultSetPrototype = new ResultSet(); 
       //$resultSetPrototype->setArrayObjectPrototype(new QuestionsTable); 
       return new TableGateway('codepro', $dbAdapter, null,  $resultSetPrototype);} 


    )); 
     please help on how to go about the servicce manager..Thanks in advance 

回答

0

試試這個:

return array(
'factories' => array(
    'Question\Model\QuestionsTable' => function($sm) { 
     return new Question\Model\QuestionsTable(); 
    }, 
    'QuestionsTableGateway' => function ($sm) { 
     $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); 
     $resultSetPrototype = new ResultSet(); 
     $resultSetPrototype->setArrayObjectPrototype($sm->get('Question\Model\QuestionTable')); 
     $tableGateway = new TableGateway('codepro', $dbAdapter, null,  $resultSetPrototype); 
     $table = new Question\Model\QuestionTable($tableGateway); 
     } 
)); 
相關問題