I have been extending the ZfcUser I got the new fields working, and they are getting added to the db via doctrine2 no problem there. But now i need to add a links between the user and a payment table and a special Role system. Im trying to put this into the Event but i cant get a hold on the doctrine service. My onBootstrap looks like this.
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');*/
});
}
Form and filter works, but i cant get access to docrtine service from within the anonymous event function. How do i do this?
Since you're doing this in onBootstrap()
the MvcEvent
has everything you need. You just need to fetch the entity manager and use
it in your anonymous function
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);
}
);
I'm assuming the entity manager was the doctrine service you were referring to, but it's the same methodology to use
other services in the scope of your anonymous functions.