我原來在幾個月前就有關ZF2在Beta 1期間向DI注入數據的問題做了另一個線索,並且認爲它不是真正可行的。現在ZF2已經發布爲2.0.0版本,並且ServiceManager默認不是DI我想我現在有同樣的問題,我正在重構。ZF2 - ServiceManager注入到84個表中...單調乏味
我有84張桌子,需要DbAdapter注入他們,我敢肯定,我必須有更好的方式來複制自己。
public function getServiceConfig()
{
return array(
'factories' => array(
'accountTable' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$table = new Model\DbTable\AccountTable($dbAdapter);
return $table;
},
'userTable' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$table = new Model\DbTable\UserTable($dbAdapter);
return $table;
},
// another 82 tables of the above
)
)
}
通過EventsManager和ServiceManager,我不知道我的位置在獲取我的應用程序的實例/資源。
感謝,大教堂
嗯,這似乎是硬編碼到框架中。在Zend \ Mvc \ Service \ ServiceManagerConfig.php#configureServiceManager($ sm){serviceManager-> addInitializer(// stuff) } 因此,這將偵聽使用AwareInterface創建任何內容並將其注入到那裏。我想我必須添加我的DbAware作爲初始化器 –