简体   繁体   中英

Zend\Db\TableGateway - my config is in database.local.php Zend Framework 2

I have the following method in my Module.php

 public function getServiceConfig()
 {
     return array(
         'factories' => array(
             'Application\Model\VehiclesTable' =>  function($sm) {
                 $tableGateway = $sm->get('VehiclesTableGateway');
                 $table = new VehiclesTable($tableGateway);
                 return $table;
             },
             'ApplicationTableGateway' => function ($sm) {
                 $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                 $resultSetPrototype = new ResultSet();
                 $resultSetPrototype->setArrayObjectPrototype(new Vehicles());
                 return new TableGateway('vehicles', $dbAdapter, null, $resultSetPrototype);
             },
         ),
     );
 }

But I am getting this error:

Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for VehiclesTableGateway

My config/autoload/database.local.php file looks like:

$dbParams = array(
    'database'  => 'zf-skeleton',
    'username'  => 'root',
    'password'  => 'root',
    'hostname'  => 'localhost',
    // buffer_results - only for mysqli buffered queries, skip for others
    'options' => array('buffer_results' => true)
);

return array(
    'service_manager' => array(
        'factories' => array(
            'Zend\Db\Adapter\Adapter' => function ($sm) use ($dbParams) {
                $adapter = new BjyProfiler\Db\Adapter\ProfilingAdapter(array(
                    'driver'    => 'pdo',
                    'dsn'       => 'mysql:dbname='.$dbParams['database'].';host='.$dbParams['hostname'],
                    'database'  => $dbParams['database'],
                    'username'  => $dbParams['username'],
                    'password'  => $dbParams['password'],
                    'hostname'  => $dbParams['hostname'],
                ));

                if (php_sapi_name() == 'cli') {
                    $logger = new Zend\Log\Logger();
                    // write queries profiling info to stdout in CLI mode
                    $writer = new Zend\Log\Writer\Stream('php://output');
                    $logger->addWriter($writer, Zend\Log\Logger::DEBUG);
                    $adapter->setProfiler(new BjyProfiler\Db\Profiler\LoggingProfiler($logger));
                } else {
                    $adapter->setProfiler(new BjyProfiler\Db\Profiler\Profiler());
                }
                if (isset($dbParams['options']) && is_array($dbParams['options'])) {
                    $options = $dbParams['options'];
                } else {
                    $options = array();
                }
                $adapter->injectProfilingStatementPrototype($options);
                return $adapter;
            },
        ),
    ),
);

I would like to keep my database.local.php file as it is but still be able to create a Table Gateway for my Module, therefore I dont think I need:

     'factories' => array(
         'Application\Model\VehiclesTable' =>  function($sm) {
             $tableGateway = $sm->get('VehiclesTableGateway');
             $table = new VehiclesTable($tableGateway);
             return $table;
         },

Can somebody point me in the right direction as to how to do this?

You don't have a 'VehiclesTableGateway' in the config file shown.

2 solutions.

If you wanted to refer to the 'ApplicationTableGateway' only change the reference of the 'VehiclesTableGateway' with 'ApplicationTableGateway' .

If you want a specific tableGateway you should update your configuration like this :

public function getServiceConfig()
 {
     return array(
         'factories' => array(
             'Application\Model\VehiclesTable' =>  function($sm) {
                 $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                 $resultSetPrototype = new ResultSet();
                 $resultSetPrototype->setArrayObjectPrototype(new Vehicles());
                 $tableGateway = new TableGateway('vehicles', $dbAdapter, null, $resultSetPrototype);
                 $table = new VehiclesTable($tableGateway);
                 return $table;
             },
             'ApplicationTableGateway' => function ($sm) {
                 $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                 $resultSetPrototype = new ResultSet();
                 $resultSetPrototype->setArrayObjectPrototype(new Vehicles());
                 return new TableGateway('vehicles', $dbAdapter, null, $resultSetPrototype);
             },
         ),
     );
 } 

I'm not really sure about your 'ApplicationTableGateway' . It seems weird to have a "global" tableGateway.

Moreover, i'd sugget that you remove those anonymous function in the configuration file and replace them with real factories class. This is more effective for page loading because anonymous function prevent zend framework from creating a cached file of the configuration (but it's not mandatory to get things to work).

Hope this helped,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM