我的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;
},
有人能为我指明正确的方向吗?
显示的配置文件中没有'VehiclesTableGateway'
。
2个解决方案。
如果要引用'ApplicationTableGateway'
,请将'VehiclesTableGateway'
的引用更改为'ApplicationTableGateway'
。
如果你想要一个特定的表网关,你应该像这样更新你的配置:
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'
。有一个"全局"表网关似乎很奇怪。
此外,我建议您删除配置文件中的那些匿名函数,并将其替换为真正的工厂类。这对于页面加载更有效,因为匿名函数阻止zend框架创建配置的缓存文件(但这并不是必须的)。
希望这有帮助,