使用Zend \ Db \ TableGateway \ TableGateway

时间:2013-12-10 09:58:23

标签: zend-framework

我正在尝试创建Zend \ Db \ 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

1 个答案:

答案 0 :(得分: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);
        }
));