我一直在扩展ZfcUser我有新的字段工作,他们通过doctrine2添加到数据库没有问题。 但现在我需要在用户和支付表以及特殊的角色系统之间添加链接。 我试图把它放到事件中,但我无法控制学说服务。 我的onBootstrap看起来像这样。
public function onBootstrap( MVCEvent $e )
{
$eventManager = $e->getApplication()->getEventManager();
$em = $eventManager->getSharedManager();
$em->attach(
'ZfcUser\Form\RegisterFilter',
'init',
function( $e )
{
$filter = $e->getTarget();
// do your form filtering here
new Register\Filter($filter);
}
);
// custom form fields
$em->attach(
'ZfcUser\Form\Register',
'init',
function($e)
{
/* @var $form \ZfcUser\Form\Register */
$form = $e->getTarget();
new Register\Form($form);
}
);
// here's the storage bit
$zfcServiceEvents = $e->getApplication()->getServiceManager()->get('zfcuser_user_service')->getEventManager();
$doctrineObjectManager = $e->getApplication()->getServiceManager()->get('Doctrine\ORM\EntityManager');
var_dump($e->getApplication()->getServiceManager()->get('zfcuser_user_service')->getEventManager()->getEvents());
$zfcServiceEvents->attach('register', function($e){
/* @var $user \User\Entity\User */
//$form = $e->getParam('form');
//print_r($form);
//echo get_class($e);
new Register\Storage($e);
break;
});
// you can even do stuff after it stores
$zfcServiceEvents->attach('register.post', function($e) {
/*$user = $e->getParam('user');*/
});
}
表单和过滤器有效,但我无法从匿名事件函数中访问docrtine服务。我该怎么做?
答案 0 :(得分:3)
由于您在onBootstrap()
执行此操作,因此MvcEvent
拥有您需要的一切。您只需要获取实体管理器并在匿名函数中use
public function onBootstrap( MVCEvent $e )
{
$eventManager = $e->getApplication()->getEventManager();
$em = $eventManager->getSharedManager();
// fetch the entity manager
$sm = $e->getApplication()->getServiceManager();
$entityManager = $sm->get('Doctrine\ORM\EntityManager');
$em->attach(
'ZfcUser\Form\RegisterFilter',
'init',
// use the entity manager in your function
function( $e ) use ($entityManager);
{
$filter = $e->getTarget();
// variable $entityManager is now available in this scope
// so we could for example inject it into the filter like so
new Register\Filter($filter, $entityManager);
}
);
我假设实体经理是您所指的学说服务,但它与匿名函数范围内的use
其他服务的方法相同。