title: zf2: how to get Service Locator in Model
body: <p>I tried some ideas from other posts and examples but I still haven't a solution.
  I can't recieve the servicelocator in my Model.</p>
  
  <p>my Module.php contains the following factory in function getServiceConfig():</p>
  
  <pre><code>'Backend\Model\Beuser' =&gt; function($sm){
                       $serviceLocator = $sm-&gt;getServiceLocator();
                       return new \Backend\Model\Beuser($serviceLocator); 
                 },
  </code></pre>
  
  <p>my model Beuser.php looks like:</p>
  
  <pre><code>namespace Backend\Model;
  
  use Zend\InputFilter\Factory as InputFactory;
  use Zend\InputFilter\InputFilter;
  use Zend\InputFilter\InputFilterAwareInterface;
  use Zend\InputFilter\InputFilterInterface;
  
  
  use Zend\ServiceManager\ServiceLocatorAwareInterface;
  use Zend\ServiceManager\ServiceLocatorInterface;
  
  class Beuser implements ServiceLocatorAwareInterface
  {
  
      public $idx_be_user;
      public $be_user_email;
      public $be_user_password;
      public $be_user_firstname;
      public $be_user_lastname;
      public $be_user_phone;
      public $be_user_photo;
      public $be_user_shorttext;
  
       protected $inputFilter;    
  
       protected $serviceLocator;
  
      public function setServiceLocator(ServiceLocatorInterface $sl)
      {
          $this-&gt;serviceLocator = $sl;
          return $this;
      }
  
      public function getServiceLocator()
      {
          return $this-&gt;serviceLocator;
      }
  
      public function exchangeArray($data)
      {
  
          $this-&gt;idx_be_user          = (!empty($data['idx_be_user'])) ? $data['idx_be_user'] : null;
          $this-&gt;be_user_email        = (!empty($data['be_user_email'])) ? $data['be_user_email'] : null;
          $this-&gt;be_user_password  = (!empty($data['be_user_password'])) ? $data['be_user_password'] : null;
          $this-&gt;be_user_firstname = (!empty($data['be_user_firstname'])) ? $data['be_user_firstname'] : null;
          $this-&gt;be_user_lastname  = (!empty($data['be_user_lastname'])) ? $data['be_user_lastname'] : null;
          $this-&gt;be_user_phone        = (!empty($data['be_user_phone'])) ? $data['be_user_phone'] : null;
          $this-&gt;be_user_photo        = (!empty($data['be_user_photo'])) ? $data['be_user_photo'] : null;
          $this-&gt;be_user_shorttext = (!empty($data['be_user_shorttext'])) ? $data['be_user_shorttext'] : null;
  
      }
  
       public function getArrayCopy()
      {
          return get_object_vars($this);
      }
  
       public function setInputFilter(InputFilterInterface $inputFilter)
      {
          throw new \Exception("Not used");
      }
  
      public function getInputFilter()
      {
  
           var_dump($this-&gt;getServiceLocator());
           die();
          if (!$this-&gt;inputFilter) {
              $inputFilter = new InputFilter();
              $factory     = new InputFactory();
  
              $inputFilter-&gt;add($factory-&gt;createInput(array(
                  'name'     =&gt; 'idx_be_user',
                  'required' =&gt; true,
                  'filters'  =&gt; array(
                      array('name' =&gt; 'Int'),
                  ),
              )));
  
              $inputFilter-&gt;add($factory-&gt;createInput(array(
                  'name'     =&gt; 'be_user_email',
                  'required' =&gt; true,
                  'filters'  =&gt; array(
                      array('name' =&gt; 'StripTags'),
                      array('name' =&gt; 'StringTrim'),
                  ),
                  'validators' =&gt; array(
                      array(
                          'name'    =&gt; 'StringLength',
                          'options' =&gt; array(
                              'encoding' =&gt; 'UTF-8',
                              'min'      =&gt; 1,
                              'max'      =&gt; 100,
                          ),
                      ),
                           /*
                            array(
                                'name'    =&gt; 'Db\NoRecordExists',
                                'options' =&gt; array(
                                      'table'     =&gt; 'users',
                                          'field'     =&gt; 'email',
                              //      'adapter'   =&gt; $this-&gt;getServiceLocator()-&gt;get('dbAdapter'),
                                ),
                           ), 
                            */
  
                  ),
              )));
  
              $inputFilter-&gt;add($factory-&gt;createInput(array(
                  'name'     =&gt; 'be_user_password',
                  'required' =&gt; true,
                  'filters'  =&gt; array(
                      array('name' =&gt; 'StripTags'),
                      array('name' =&gt; 'StringTrim'),
                  ),
                  'validators' =&gt; array(
                      array(
                          'name'    =&gt; 'StringLength',
                          'options' =&gt; array(
                              'encoding' =&gt; 'UTF-8',
                              'min'      =&gt; 6,
                              'max'      =&gt; 100,
                          ),
                      ),  
                  ),
              )));
  
              $inputFilter-&gt;add($factory-&gt;createInput(array(
                  'name'     =&gt; 'be_user_firstname',
                  'required' =&gt; true,
                  'filters'  =&gt; array(
                      array('name' =&gt; 'StripTags'),
                      array('name' =&gt; 'StringTrim'),
                  ),
                  'validators' =&gt; array(
                      array(
                          'name'    =&gt; 'StringLength',
                          'options' =&gt; array(
                              'encoding' =&gt; 'UTF-8',
                              'min'      =&gt; 1,
                              'max'      =&gt; 100,
                          ),
                      ),
                  ),
              )));
  
              $inputFilter-&gt;add($factory-&gt;createInput(array(
                  'name'     =&gt; 'be_user_lastname',
                  'required' =&gt; true,
                  'filters'  =&gt; array(
                      array('name' =&gt; 'StripTags'),
                      array('name' =&gt; 'StringTrim'),
                  ),
                  'validators' =&gt; array(
                      array(
                          'name'    =&gt; 'StringLength',
                          'options' =&gt; array(
                              'encoding' =&gt; 'UTF-8',
                              'min'      =&gt; 1,
                              'max'      =&gt; 100,
                          ),
                      ),
                  ),
              )));
  
              $inputFilter-&gt;add($factory-&gt;createInput(array(
                  'name'     =&gt; 'be_user_phone',
                  'required' =&gt; true,
                  'filters'  =&gt; array(
                      array('name' =&gt; 'StripTags'),
                      array('name' =&gt; 'StringTrim'),
                  ),
                  'validators' =&gt; array(
                      array(
                          'name'    =&gt; 'StringLength',
                          'options' =&gt; array(
                              'encoding' =&gt; 'UTF-8',
                              'min'      =&gt; 1,
                              'max'      =&gt; 100,
                          ),
                      ),
                  ),
              )));
  
  
                  $inputFilter-&gt;add(
                  $factory-&gt;createInput(array(
                      'name'     =&gt; 'be_user_photo',
                      'required' =&gt; false,
                  ))
              );
  
  
              $inputFilter-&gt;add($factory-&gt;createInput(array(
                  'name'     =&gt; 'be_user_shorttext',
                  'required' =&gt; false,
                  'filters'  =&gt; array(
                      array('name' =&gt; 'StripTags'),
                      array('name' =&gt; 'StringTrim'),
                  ),
                  'validators' =&gt; array(
                      array(
                          'name'    =&gt; 'StringLength',
                          'options' =&gt; array(
                              'encoding' =&gt; 'UTF-8',
                              'min'      =&gt; 5,
                              'max'      =&gt; 255,
                          ),
                      ),
                  ),
              )));
  
              $this-&gt;inputFilter = $inputFilter;
          }
  
          return $this-&gt;inputFilter;
      }
  
  }
  </code></pre>
  
  <p>But
   <code>var_dump($this-&gt;getServiceLocator());</code> 
  returns NULL. Thats the reasion why any call of <code>$this-&gt;getServiceLocator()-&gt;get('something')</code> 
  creates an
   <code>PHP Fatal error:  Call to a member function get()[...]</code></p>
  
  <p>Could you help me finding the problem?</p>
  
  <p>PS: I also tried following factory:</p>
  
  <pre><code> 'Backend\Model\Beuser' =&gt; function($sm){
          $dbAdapter = $sm-&gt;get('Zend\Db\Adapter\Adapter');
          $beuser = new \Backend\Model\Beuser();
          $beuser-&gt;setDbAdapter($dbAdapter);
          return $beuser;
      },
  </code></pre>
  
  <p>my module.php:</p>
  
  <pre><code>namespace Backend;
  
  use Zend\Db\ResultSet\ResultSet;
  use Zend\Db\TableGateway\TableGateway;
  use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
  use Zend\Authentication\Storage;
  use Zend\Mvc\Controller\AbstractActionController;
  use Zend\Authentication\AuthenticationService;
  use Zend\Authentication\Adapter\DbTable as AuthAdapter;
  use Zend\Authentication\Result as Result;
  use Zend\Session\Container; // We need this when using sessions
  
  //Be User
  use Backend\Model\Beuser;
  use Backend\Model\BeuserTable;
  
  
  class Module implements AutoloaderProviderInterface
  {
  
      public function getAutoloaderConfig()
      {
           return array(
               'Zend\Loader\ClassMapAutoloader' =&gt; array(
                  __DIR__ . '/autoload_classmap.php'
               ),
               'Zend\Loader\StandardAutoloader' =&gt; array(
                  'namespaces' =&gt; array(
                      __NAMESPACE__ =&gt; __DIR__ . '/src/' . __NAMESPACE__,
                  ),
               ),
           );
       }
  
      public function getConfig()
      {
      return include __DIR__ . '/config/module.config.php';
      }
  
      public function getServiceConfig()
     {
          return array(
  
                 //factories
              'factories'=&gt;array(
  
                      //Storage
                     'BackendStorage' =&gt; function($sm){
                          return new BackendStorage('backend'); 
                      },                   
                      //DB Adapter
                      'dbAdapter' =&gt; function($sm) {          
                           $dbAdapter = $sm-&gt;get('Zend\Db\Adapter\Adapter');                       
                           return $dbAdapter;                      
                 },                             
                      //nur DB
                      'db'  =&gt; function($sm) {
                          $dba= $sm-&gt;get('dbAdapter');
                          $db = new db\Db($dba);
                          return $db;
                      },                   
                      //mache Auth Info verfuegbar                            
                      'AuthService' =&gt; function($sm) {    
                          //$dbAdapter = $sm-&gt;get('Zend\Db\Adapter\Adapter');                                             
                          $dbTableAuthAdapter  = new AuthAdapter($sm-&gt;get('dbAdapter'),'tbl_be_user','be_user_email','be_user_password', 'MD5(?)');               
                          //$authService = new AuthenticationService();               
                          $authService = new AuthenticationService(new Storage\Session('Auth_Backend'));
                          $authService-&gt;setAdapter($dbTableAuthAdapter);
                          return $authService;                    
  
                      },  
                      //Navigation
                      'NavigationFactory' =&gt; 'Backend\Service\NavigationFactory', 
  
                      // Backend User       
                      'Backend\Model\BeuserTable' =&gt;  function($sm) {
                      $tableGateway = $sm-&gt;get('BeuserTableGateway');
                      $table = new BeuserTable($tableGateway);
                      return $table;
                  },                              
                 'BeuserTableGateway' =&gt; function ($sm) {
                      $dbAdapter = $sm-&gt;get('dbAdapter');
                      $resultSetPrototype = new ResultSet();
                      $resultSetPrototype-&gt;setArrayObjectPrototype(new Beuser());
                      return new TableGateway('tbl_be_user', $dbAdapter, null, $resultSetPrototype);
                  },  
  
                      'Backend\Model\Beuser' =&gt; function($sm){
                          $model = new \Backend\Model\Beuser(); 
                          $model-&gt;setServiceLocator($sm);
  
                          return $model;
                      },
  
              ),
  
  
  
          );                
  
      }
  
       public function getViewHelperConfig()
      {
          return array(
              'invokables' =&gt; array(
                  'PageTitle' =&gt; 'Backend\ViewHelper\PageTitle',  
              ),
              'factories' =&gt; array(
                   //hasIdentity true or false
                   'AuthStatusFactory' =&gt; function ($serviceManager) {
                        // Get the service locator 
                  $serviceLocator = $serviceManager-&gt;getServiceLocator();
                  // pass it to your helper 
                  return new \Backend\ViewHelper\AuthStatusFactory($serviceLocator);
               },
                   'BeUserName' =&gt; function ($serviceManager) {
                        // Get the service locator 
                  $serviceLocator = $serviceManager-&gt;getServiceLocator();
                  // pass it to your helper 
                  return new \Backend\ViewHelper\BeUserName($serviceLocator);
              },
              ) 
          );
      }
  
  
  }
  </code></pre>