2017-03-05 1 views
0

Je suis nouveau à ZF2. Après quelques jours d'essayer de comprendre comment tout cela devrait fonctionner, je n'ai pas réussi à comprendre comment appeler TableGateway Model from Service.Zend Framework 2 appel TableGateway en service

J'ai donc contrôleur:

class SubscriberController extends AbstractActionController 
{ 
/** 
* @var \Subscriber\Service\SubscriberServiceInterface 
*/ 
private $subscriberService; 

/** 
* @param $subscriberService 
*/ 
public function __construct(SubscriberServiceInterface $subscriberService) 
{ 
    $this->subscriberService = $subscriberService; 
} 

Factroy pour ce contrôleur:

class SubscriberControllerFactory implements FactoryInterface 
{ 
/** 
* Returns ArchiveController instance. 
* 
* @param ServiceLocatorInterface $serviceLocator 
* @return SubscriberController 
* @override 
**/ 
public function createService(ServiceLocatorInterface $serviceLocator) 
{ 
    $sm = $serviceLocator->getServiceLocator(); 

    return new SubscriberController(
     $sm->get('Subscriber\Service\SubscriberServiceInterface') 
    ); 
} 

Certains SubscriberTable:

class SubscriberTable 
{ 
protected $tableGateway; 

public function __construct(TableGateway $tableGateway) 
{ 
    $this->tableGateway = $tableGateway; 
} 

public function fetchAll() 
{ 
    $resultSet = $this->tableGateway->select(); 
    return $resultSet; 
} 

et service dans lequel je veux exemple SubscriberTable et faire une certaine logique. Mais je ne peux pas comprendre comment dois-je appeler cette instance dans SubscriberService et définir le dbAdapter pour SubscriberTable

Répondre

1
First implement servicelocator interface and define get and set locator functions to your service like this. 

    use Zend\ServiceManager\ServiceLocatorAwareInterface; 
    use Zend\ServiceManager\ServiceLocatorInterface; 

    class Yourservice implements ServiceLocatorAwareInterface{ 

    function test(){ 
     $this->getSubscriberTable->fetchAll(); // call to subscriber table functions 
    }  

    /** 
    * @table gateway Call 
    **/ 
    public function getSubscriberTable() 
    { 
     if (!$this->SubscriberTable) { 
      $sm = $this->getServiceLocator(); 
      $this->SubscriberTable = $sm->get('Application\Model\SubscriberTable'); 
     } 
     return $this->SubscriberTable; 
    } 

    public function setServiceLocator(ServiceLocatorInterface $serviceLocator) 
    { 
     $this->serviceLocator = $serviceLocator; 
    } 

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

espère que cela vous aidera.