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

时间:2016-04-07 14:20:20

标签: php zend-framework zend-framework2

我的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文件,但仍然可以为我的模块创建一个表网关,因此我认为我不需要:

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

有人可以指出我如何做到这一点的正确方向吗?

1 个答案:

答案 0 :(得分:2)

您显示的配置文件中没有'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'不太确定。有一个"全球"似乎很奇怪。 tableGateway。

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

希望这有帮助,