1

我一直在尝试创建一个实例,Zend\Db\TableGateway但无法做到正确。这就是我的module.php

use Question\Model\QuestionsTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;

//other statements and then getServiceConfig()
public function getServiceConfig()
{
    return array(
        'factories' => array(
            'Question\Model\QuestionsTable' =>  function($sm) {
                $tableGateway = $sm->get('AlbumTableGateway');
                $table = new QuestionsTable($tableGateway);
                return $table;
            },
            'AlbumTableGateway' => function ($sm) {
                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                $resultSetPrototype = new ResultSet();
                $resultSetPrototype->setArrayObjectPrototype(new QuestionsTable());
                return new TableGateway('questions', $dbAdapter, null, $resultSetPrototype);
            },
        ),
    );
}

这是我的QuestionsTable.php文件:

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;
    }
}

这是我得到的错误: Catchable fatal error: Argument 1 passed to Question\Model\QuestionsTable::__construct() must be an instance of Zend\Db\TableGateway\TableGateway,none given.

提前致谢。

4

1 回答 1

1

嗨,我认为您应该将表类与原型类分开。
作为解决方案,您可以在 Question\Model\Questions 中添加另一个类 Questions 并将其用作原型

$resultSetPrototype->setArrayObjectPrototype(new Questions()); //instead of QuestionsTable

并且您可以按照专辑 tuto中的数据库和模型中所述的相同方式进行操作

于 2013-12-10T19:21:34.160 回答