Get service manager instance inside event listner zend framework2

I have a mapper class "User" with findAll() function.

public function findAll()
 {        
  /**
  * Fetch records from table 'User'

  *@author Administrator
  */

    $this->getEventManager()->trigger('find', $this, array('entity' => $entity));
    return $entity;

}

Here I am triggering a event which is listening by the user's module class

public function onBootstrap(MvcEvent $event)
{

    $eventManager       = $event->getApplication()->getEventManager();
    $sharedEventManager = $eventManager->getSharedManager();

    $sm = $event->getApplication()->getServiceManager();


    $sharedEventManager->attach('ZfcUser\Mapper\User', 'find', function($e) {

         /*
          Access log create object
         */

    }, 100);

}

Really I want this to add log record in to database, I have prepared a service to return a object that capable of write this record to DB.

public function getServiceConfig() {
    return array(
        'factories' => array(
               'Dblogger' => function($sm) {

                $logger = $sm->get('logger');
                $plugin = new Controller\Plugin\Log;
                $plugin->setLogger($logger);
                return $plugin;
            },
        ),
    );
}

With using this service its all I have to do is call this method

$sm = $event->getApplication()->getServiceManager();
$logger = $sm->get('Dblogger');
$logger->Debug('View the user list');

But my problem is, there is no anyway to call this service inside this closure function

 $sharedEventManager->attach('ZfcUser\Mapper\User', 'find', function($e) {

         /*
          Access log create object
         */

    }, 100);

Please help me to comeup with a solution

Answers


There may be a cleaner solution, but i simply use it:

$sEM->attach('Namespace', 'eventName', function(Event $e) use ($serviceManager) {
    $serviceManager->get('fooBar');
});

public function onBootstrap(MvcEvent $event)
{

    $eventManager       = $event->getApplication()->getEventManager();
    $sharedEventManager = $eventManager->getSharedManager();

    $sm = $event->getApplication()->getServiceManager();


    $sharedEventManager->attach('ZfcUser\Mapper\User', 'find', function($e) use ($sm) {

         /*
          Access log create object
         */
         $sm->....

    }, 100);

}

Need Your Help

Load an image into a JPanel in an applet

java image swing paintcomponent jfilechooser

base on this thread :Java: Load image from file, edit and add to JPanel

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.