2013-07-03 4 views
7

में सेवा लोकेटर कैसे प्राप्त करें मैंने अन्य पदों और उदाहरणों से कुछ विचारों की कोशिश की लेकिन मुझे अभी भी समाधान नहीं मिला है। मैं अपने मॉडल में servicelocator प्राप्त नहीं कर सकता।zf2: मॉडल

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->serviceLocator = $sl; 
     return $this; 
    } 

    public function getServiceLocator() 
    { 
     return $this->serviceLocator; 
    } 

    public function exchangeArray($data) 
    { 

     $this->idx_be_user   = (!empty($data['idx_be_user'])) ? $data['idx_be_user'] : null; 
     $this->be_user_email  = (!empty($data['be_user_email'])) ? $data['be_user_email'] : null; 
     $this->be_user_password = (!empty($data['be_user_password'])) ? $data['be_user_password'] : null; 
     $this->be_user_firstname = (!empty($data['be_user_firstname'])) ? $data['be_user_firstname'] : null; 
     $this->be_user_lastname = (!empty($data['be_user_lastname'])) ? $data['be_user_lastname'] : null; 
     $this->be_user_phone  = (!empty($data['be_user_phone'])) ? $data['be_user_phone'] : null; 
     $this->be_user_photo  = (!empty($data['be_user_photo'])) ? $data['be_user_photo'] : null; 
     $this->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->getServiceLocator()); 
     die(); 
     if (!$this->inputFilter) { 
      $inputFilter = new InputFilter(); 
      $factory  = new InputFactory(); 

      $inputFilter->add($factory->createInput(array(
       'name'  => 'idx_be_user', 
       'required' => true, 
       'filters' => array(
        array('name' => 'Int'), 
       ), 
      ))); 

      $inputFilter->add($factory->createInput(array(
       'name'  => 'be_user_email', 
       'required' => true, 
       'filters' => array(
        array('name' => 'StripTags'), 
        array('name' => 'StringTrim'), 
       ), 
       'validators' => array(
        array(
         'name' => 'StringLength', 
         'options' => array(
          'encoding' => 'UTF-8', 
          'min'  => 1, 
          'max'  => 100, 
         ), 
        ), 
         /* 
          array(
           'name' => 'Db\NoRecordExists', 
           'options' => array(
            'table'  => 'users', 
             'field'  => 'email', 
          //  'adapter' => $this->getServiceLocator()->get('dbAdapter'), 
          ), 
         ), 
          */ 

       ), 
      ))); 

      $inputFilter->add($factory->createInput(array(
       'name'  => 'be_user_password', 
       'required' => true, 
       'filters' => array(
        array('name' => 'StripTags'), 
        array('name' => 'StringTrim'), 
       ), 
       'validators' => array(
        array(
         'name' => 'StringLength', 
         'options' => array(
          'encoding' => 'UTF-8', 
          'min'  => 6, 
          'max'  => 100, 
         ), 
        ), 
       ), 
      ))); 

      $inputFilter->add($factory->createInput(array(
       'name'  => 'be_user_firstname', 
       'required' => true, 
       'filters' => array(
        array('name' => 'StripTags'), 
        array('name' => 'StringTrim'), 
       ), 
       'validators' => array(
        array(
         'name' => 'StringLength', 
         'options' => array(
          'encoding' => 'UTF-8', 
          'min'  => 1, 
          'max'  => 100, 
         ), 
        ), 
       ), 
      ))); 

      $inputFilter->add($factory->createInput(array(
       'name'  => 'be_user_lastname', 
       'required' => true, 
       'filters' => array(
        array('name' => 'StripTags'), 
        array('name' => 'StringTrim'), 
       ), 
       'validators' => array(
        array(
         'name' => 'StringLength', 
         'options' => array(
          'encoding' => 'UTF-8', 
          'min'  => 1, 
          'max'  => 100, 
         ), 
        ), 
       ), 
      ))); 

      $inputFilter->add($factory->createInput(array(
       'name'  => 'be_user_phone', 
       'required' => true, 
       'filters' => array(
        array('name' => 'StripTags'), 
        array('name' => 'StringTrim'), 
       ), 
       'validators' => array(
        array(
         'name' => 'StringLength', 
         'options' => array(
          'encoding' => 'UTF-8', 
          'min'  => 1, 
          'max'  => 100, 
         ), 
        ), 
       ), 
      ))); 


       $inputFilter->add(
       $factory->createInput(array(
        'name'  => 'be_user_photo', 
        'required' => false, 
       )) 
      ); 


      $inputFilter->add($factory->createInput(array(
       'name'  => 'be_user_shorttext', 
       'required' => false, 
       'filters' => array(
        array('name' => 'StripTags'), 
        array('name' => 'StringTrim'), 
       ), 
       'validators' => array(
        array(
         'name' => 'StringLength', 
         'options' => array(
          'encoding' => 'UTF-8', 
          'min'  => 5, 
          'max'  => 255, 
         ), 
        ), 
       ), 
      ))); 

      $this->inputFilter = $inputFilter; 
     } 

     return $this->inputFilter; 
    } 

} 

लेकिन var_dump($this->getServiceLocator()); रिटर्न नल:

'Backend\Model\Beuser' => function($sm){ 
        $serviceLocator = $sm->getServiceLocator(); 
        return new \Backend\Model\Beuser($serviceLocator); 
       }, 

अपने मॉडल Beuser.php की तरह दिखता है:

मेरी Module.php समारोह getServiceConfig (में निम्नलिखित कारखाना) शामिल हैं। reasion क्यों $this->getServiceLocator()->get('something') के किसी भी कॉल एक PHP Fatal error: Call to a member function get()[...]

तुम मुझे समस्या खोजने में मदद कर सकते हैं बनाता है thats?

पुनश्च:

'Backend\Model\Beuser' => function($sm){ 
     $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); 
     $beuser = new \Backend\Model\Beuser(); 
     $beuser->setDbAdapter($dbAdapter); 
     return $beuser; 
    }, 

मेरी module.php:

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' => array(
       __DIR__ . '/autoload_classmap.php' 
      ), 
      'Zend\Loader\StandardAutoloader' => array(
       'namespaces' => array(
        __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, 
       ), 
      ), 
     ); 
    } 

    public function getConfig() 
    { 
    return include __DIR__ . '/config/module.config.php'; 
    } 

    public function getServiceConfig() 
    { 
     return array(

       //factories 
      'factories'=>array(

        //Storage 
        'BackendStorage' => function($sm){ 
         return new BackendStorage('backend'); 
        },     
        //DB Adapter 
        'dbAdapter' => function($sm) {   
         $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');      
         return $dbAdapter;      
       },        
        //nur DB 
        'db' => function($sm) { 
         $dba= $sm->get('dbAdapter'); 
         $db = new db\Db($dba); 
         return $db; 
        },     
        //mache Auth Info verfuegbar        
        'AuthService' => function($sm) {  
         //$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');            
         $dbTableAuthAdapter = new AuthAdapter($sm->get('dbAdapter'),'tbl_be_user','be_user_email','be_user_password', 'MD5(?)');    
         //$authService = new AuthenticationService();    
         $authService = new AuthenticationService(new Storage\Session('Auth_Backend')); 
         $authService->setAdapter($dbTableAuthAdapter); 
         return $authService;      

        }, 
        //Navigation 
        'NavigationFactory' => 'Backend\Service\NavigationFactory', 

        // Backend User  
        'Backend\Model\BeuserTable' => function($sm) { 
        $tableGateway = $sm->get('BeuserTableGateway'); 
        $table = new BeuserTable($tableGateway); 
        return $table; 
       },        
       'BeuserTableGateway' => function ($sm) { 
        $dbAdapter = $sm->get('dbAdapter'); 
        $resultSetPrototype = new ResultSet(); 
        $resultSetPrototype->setArrayObjectPrototype(new Beuser()); 
        return new TableGateway('tbl_be_user', $dbAdapter, null, $resultSetPrototype); 
       }, 

        'Backend\Model\Beuser' => function($sm){ 
         $model = new \Backend\Model\Beuser(); 
         $model->setServiceLocator($sm); 

         return $model; 
        }, 

      ), 



     );     

    } 

    public function getViewHelperConfig() 
    { 
     return array(
      'invokables' => array(
       'PageTitle' => 'Backend\ViewHelper\PageTitle', 
      ), 
      'factories' => array(
       //hasIdentity true or false 
       'AuthStatusFactory' => function ($serviceManager) { 
         // Get the service locator 
       $serviceLocator = $serviceManager->getServiceLocator(); 
       // pass it to your helper 
       return new \Backend\ViewHelper\AuthStatusFactory($serviceLocator); 
      }, 
       'BeUserName' => function ($serviceManager) { 
         // Get the service locator 
       $serviceLocator = $serviceManager->getServiceLocator(); 
       // pass it to your helper 
       return new \Backend\ViewHelper\BeUserName($serviceLocator); 
      }, 
      ) 
     ); 
    } 


} 

उत्तर

7

आप इसे एक से कोई भी विद्यमान निर्माता के माध्यम से में से गुजर रहे हैं ... बस सेटर का उपयोग मैं भी कारखाने निम्नलिखित की कोशिश की। सेवा प्रबंधक ServiceLocatorInterface लागू करता है, बस इसे अपने मॉडल के माध्यम से पास करें।

'Backend\Model\Beuser' => function($sm){ 
    $model = new \Backend\Model\Beuser(); 
    $model->setServiceLocator($sm); 

    return $model; 
}, 

उदाहरण नियंत्रक में प्रवेश::

public function testAction() 
{ 
    // The service manager will then inject itself into your model 
    // when it is instantiated. 
    $myModel = $this->getServiceLocator()->get('Backend\Model\Beuser'); 

    // now you have the Service Locator injecte4d into your model .. 
    //var_dump($myModel->getServiceLocator()); 
} 

आपको यह भी सुनिश्चित उपयोग सेवा लोकेटर मॉडल आप आपके ज़रूरत से किसी उदाहरण पाने के लिए बनाने की जरूरत है, नहीं तो कुछ भी नहीं

आपकी सेवा config बदलें इंजेक्शन दिया जाएगा:

'BeuserTableGateway' => function ($sm) { 
    $dbAdapter = $sm->get('dbAdapter'); 
    $resultSetPrototype = new ResultSet(); 
    // Beuser will not have the service locator injected below.. 
    // and will return null if the object wasn't created via the service manager. 
    //$resultSetPrototype->setArrayObjectPrototype(clone $sm->get('Backend\Model\Beuser')); 
    $resultSetPrototype->setArrayObjectPrototype(new Beuser()); 

    return new TableGateway('tbl_be_user', $dbAdapter, null, $resultSetPrototype); 
}, 
+0

मैं करने के लिए सेवा config में अपने कारखाने बदल अपने सुझाव, लेकिन var_dump ($ this-> getServiceLocator()); अभी भी वापस आता है। ऐसा लगता है कि मैंने इंजेक्शन को पूरी तरह से समझ नहीं लिया है ...: - – christiana83

+0

var_dump करने के लिए आप अपने मॉडल को कहां और कैसे पकड़ रहे हैं? कृपया कोड पोस्ट करें? आप अपने लिए मॉडल प्राप्त करने के लिए सेवा लोकेटर का उपयोग कर रहे हैं? – Andrew

+0

मैं अपने मॉडल में सेवा लोकेटर का उपयोग करना चाहता हूं, हाँ ... इसकी वजह है क्योंकि मुझे सत्यापन के लिए एडाप्टर की आवश्यकता है: – christiana83

0

धन्यवाद एंड्रयू - आपके उत्तरों में शू है मुझे सही तरीका है :-) मैंने अपनी समस्या हल की। मैं सेवा मॉडल द्वारा नियंत्रक में एल्बम मॉडल प्राप्त करना भूल गया।

नियंत्रक:

public function addAction(){ 
    [...] 
    //Adapter an Beuser Model uebergeben 
    $beuser->setDbAdapter($this->getServiceLocator()->get('dbAdapter')); 
    [...] 
} 

public function editAction(){ 
    [...] 
    //Adapter an Beuser Model uebergeben 
    $beuser->setDbAdapter($this->getServiceLocator()->get('dbAdapter')); 
    [...] 
} 

मॉडल:

private $adapter; 

public function setDbAdapter($dbAdapter) { 
    $this->adapter = $dbAdapter; 
} 

public function getDbAdapter() { 
    return $this->adapter;  
} 

और अब मैं मॉडल में एडाप्टर कॉल कर सकते हैं इस तरह से

$this->getDbAdapter();