zendframework/zend-db

__construct() must be an instance of Zend\Db\TableGateway\TableGateway, none given

Closed this issue · 3 comments

Hello,

I am writing my first Zend 3 Index & Model based off TableGateway and I am getting:
Catchable fatal error: Argument 1 passed to Application\Model\UsersTable::__construct() must be an instance of Zend\Db\TableGateway\TableGateway, none given

Here is my controller:

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Model;

class IndexController extends AbstractActionController
{
    private $usersTable = null;

    public function indexAction()
    {
        $view = new ViewModel();
        $model = $this->getAlbumTable();
        $row = $model->getById(1);

        $view->setVariable('id', $row['name']);


        return $view;
    }

    public function getAlbumTable()
     {
         if (!$this->usersTable) {
             $sm = $this->getPluginManager();
             $this->usersTable = $sm->get('Application\Model\UsersTable');
         }
         return $this->usersTable;
     }
}

Model:

namespace Application\Model;

use Zend\Db\TableGateway\TableGateway;

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

    public function getById($id)
    {
         $id  = (int) $id;
         $rowset = $this->tableGateway->select(array('id' => $id));
         $row = $rowset->current();

         if (!$row) {
             throw new \Exception("Could not find row $id");
         }
         return $row;
    }
}

Module.php:

public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            )
        );
    }

    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'UsersTableGateway' => function ($sm) {
                     $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');

                     $resultSetPrototype = new ResultSet();
                     $resultSetPrototype->setArrayObjectPrototype(new User());
                     return new TableGateway('users', $dbAdapter, null, $resultSetPrototype);
                 }, 
                'Application\Model\UsersTable' =>  function($sm) {
                     exit('dbAdapter');
                     $tableGateway = $sm->get('UsersTableGateway');
                     $table = new UsersTable($tableGateway);

                     return $table;
                 },

             )
        );
    }

and module.config.php:

'controllers' => [
        'factories' => [
            Controller\IndexController::class => InvokableFactory::class,
        ],
    ],
    'view_manager' => [
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => [
            'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
            'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ],
        'template_path_stack' => [
            __DIR__ . '/../view',
        ],
        'base_path' => '/zend3/public/'
    ],

    'di' => array(
        'services' => array(
            'Application\Model\UsersTable' => 'Application\Model\UsersTable'
        )
    ),

Can someone tell me what am I doing wrong here?
On the side note, shouldn't the exception be more specific about what's wrong? I bet I am missing some registration to service manager or something around it, so why the message isn't about that instead then?
Thanks,

I've managed to fix it by updateding 'controllers' key with:

'controllers' => [
        'factories' => [
            Controller\IndexController::class => function($sm) {
                $postService = $sm->get('Application\Model\UsersTable');

                return new Controller\IndexController($postService);
            }
        ],
    ],

and by updating controller's constructor:

public function __construct(UsersTable $usersTable)
    {
         $this->usersTable = $usersTable;
    }

Is this how it should be done dependencies in Zend 3 or have I just hacked it?

One step after another:

  1. I think, you use the wrong tutorial. Please take this one: docs.zendframework.com/tutorials/getting-started
  2. The error message above has nothing to do with your fix.
  3. If you found a bug in the example code or another problem in the tutorial, please use this issue tracker. (The current issue tracker is only for the Zend\Db component.)
  4. This is an issue tracker, not a support forum. Please ask your question at Stackoverflow or IRC (irc://irc.freenode.net/zftalk).

Thanks!

Thanks for the info I will tackle other issue tracker because the error which I've got was due to invalid service manager setup.

Zend 3 should be more clever about saying what needs doing instead of just php erroring (something to bare in mind if you want to achive the same amount of popularity and use, like for Zend 1).