Question

I have been trying to create an instance of Zend\Db\TableGateway but just can't get it right. This is what I have in my 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);
            },
        ),
    );
}

This is my QuestionsTable.php file:

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

And this is the error I am getting: Catchable fatal error: Argument 1 passed to Question\Model\QuestionsTable::__construct() must be an instance of Zend\Db\TableGateway\TableGateway,none given.

Thanks in advance.

Was it helpful?

Solution

Hi I think that you should separate the table class from the prototype class.
and as a solution you can add another class Questions in Question\Model\Questions and use it as prototype

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

and you can do it the same way as described in Database and models from the album tuto

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top