Zend\Db\TableGateway - 我的配置在 database.local.php Zend Framework 2

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

我的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);
             },
         ),
     );
 }

但是我收到这个错误:

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

我的 config/autoload/database.local.php 文件如下所示:

$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;
            },
        ),
    ),
);

我想保留我的 database.local.php 文件,但仍然能够为我的模块创建一个 Table 网关,因此我认为我不需要:

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

有人可以为我指明正确的方向吗?

显示的配置文件中没有 'VehiclesTableGateway'

2 个解决方案。

如果您想引用 'ApplicationTableGateway',只需将 'VehiclesTableGateway' 的引用更改为 'ApplicationTableGateway'

如果你想要一个特定的 tableGateway,你应该像这样更新你的配置:

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

我不太确定你的 'ApplicationTableGateway'。有一个 "global" tableGateway 似乎很奇怪。

此外,我建议您删除配置文件中的那些匿名函数,并用真正的工厂替换它们class。 这对于页面加载更有效,因为匿名函数阻止 zend 框架创建配置的缓存文件(但它不是强制性的)。

希望对您有所帮助,