Zend framework Zend Framework 2正在使用的调用TableGateway

Zend framework Zend Framework 2正在使用的调用TableGateway,zend-framework,service,tablegateway,Zend Framework,Service,Tablegateway,我是ZF2的新手。经过几天的努力,我想弄清楚这些东西应该如何工作,但我无法弄清楚应该如何从服务调用TableGateway模型 所以我有控制器: class SubscriberController extends AbstractActionController { /** * @var \Subscriber\Service\SubscriberServiceInterface */ private $subscriberService; /** * @param $subscrib

我是ZF2的新手。经过几天的努力,我想弄清楚这些东西应该如何工作,但我无法弄清楚应该如何从服务调用TableGateway模型

所以我有控制器:

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

/**
 * @param $subscriberService
 */
public function __construct(SubscriberServiceInterface $subscriberService)
{
    $this->subscriberService = $subscriberService;
}
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')
    );
}
此控制器的事实:

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

/**
 * @param $subscriberService
 */
public function __construct(SubscriberServiceInterface $subscriberService)
{
    $this->subscriberService = $subscriberService;
}
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')
    );
}
一些可供订阅:

class SubscriberTable
{
protected $tableGateway;

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

public function fetchAll()
{
    $resultSet = $this->tableGateway->select();
    return $resultSet;
}
和服务,我想在其中获得可订阅实例并进行一些逻辑。但我不知道应该如何在SubscriberService中调用此实例并为SubscriberTable设置DbAdapter

    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;
    }
}
希望它能帮助你