我尝试使用ZF2 Form和Doctirine。但我在C:\Projects\eperde\vendor\zendframework\zendfframework\library\Zend\ServiceManager.php的第行出现了一个错误"致命错误:未捕获的异常'Zend\ServicesManager\exception\InvalidArgumentException',消息为'$initializer should be callable'"我的Form类路径为true。它是Admin\Form\CategoryForm。
你能帮我吗?怎么了?
public function getFormElementConfig(){
return array(
'initializers' => array(
'invokables' => array(
'CategoryForm' => 'AdminFormCategoryForm',
),
'ObjectManagerInitializer' => function ($element, $formElements) {
if ($element instanceof ObjectManagerAwareInterface) {
$services = $formElements->getServiceLocator();
$entityManager = $services->get('DoctrineORMEntityManager');
$element->setObjectManager($entityManager);
}
},
),
);
}
我认为这正是您想要做的:
为categoryform
创建一个指向AdminFormCategoryForm
的别名。添加初始值设定项以注入ObjectManager。我认为工厂会是一种更优雅的方式来做到这一点!
module.config.php(或Module::getConfig()
):
'form_elements' => array(
'aliases' => array(
'categoryform' => 'AdminFormCategoryForm',
),
'initializers' => array(
'ObjectManagerInitializer' => function ($element, $pluginManager) {
if ($element instanceof DoctrineModulePersistenceObjectManagerAwareInterface) {
$serviceLocator = $pluginManager->getServiceLocator();
$entityManager = $serviceLocator->get('DoctrineORMEntityManager');
$element->setObjectManager($entityManager);
}
},
),
),