Zend Framework 2在服务中调用TableGateway



我是ZF2的新手。经过几天试图弄清楚所有这些内容应该如何工作的东西,我无法弄清楚如何从服务中调用TableGateway模型。

所以我有控制器:

class SubscriberController extends AbstractActionController
{
/**
 * @var SubscriberServiceSubscriberServiceInterface
 */
private $subscriberService;
/**
 * @param $subscriberService
 */
public function __construct(SubscriberServiceInterface $subscriberService)
{
    $this->subscriberService = $subscriberService;
}

该控制器的Factroy:

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('SubscriberServiceSubscriberServiceInterface')
    );
}

一些可订阅:

class SubscriberTable
{
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
    $this->tableGateway = $tableGateway;
}
public function fetchAll()
{
    $resultSet = $this->tableGateway->select();
    return $resultSet;
}

和我想获得可订阅实例并制作一些逻辑的服务。但是我不知道我应该如何在订阅服务中调用此实例,并为订户设置dbadapter

    First implement servicelocator interface and define get and set locator functions to your service like this.
    use ZendServiceManagerServiceLocatorAwareInterface;
    use ZendServiceManagerServiceLocatorInterface;
    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('ApplicationModelSubscriberTable');
       }
       return $this->SubscriberTable;
    }
    public function setServiceLocator(ServiceLocatorInterface $serviceLocator) 
    {
        $this->serviceLocator = $serviceLocator;
    }
    public function getServiceLocator() 
    {
        return $this->serviceLocator;
    }
}

希望它对您有帮助。

最新更新