我无法在另一个控制器中访问我的模块模型表


ZF2 I cant access my module modle table in another controller

这是我的第一堂课

<?php 
namespace Config'Controller;
use Zend'Mvc'Controller'AbstractActionController;
use Zend'View'Model'ViewModel;
use Config'Model'Config;
use Config'Form'ConfigForm;
class ConfigController extends AbstractActionController
{ 
protected $configTable;

public function indexAction()
{
    $this->getSMTPConfigTable();
    return new ViewModel(array(
            'config' => $this->getConfigTable()->fetchAll(),
    ));
}
public function addAction()
{
    $form = new ConfigForm();
    $form->get('submit')->setValue('Add');
    $request = $this->getRequest();
    if ($request->isPost()) {
        $config = new Config();
        $form->setInputFilter($config->getInputFilter());
        $form->setData($request->getPost());
        if ($form->isValid()) {
            $config->exchangeArray($form->getData());
            $this->getConfigTable()->saveConfig($config);
            return $this->redirect()->toRoute('zfcadmin/config');
        }
    }
    return array('form' => $form);
}
public function editAction()
{
    $id = (int) $this->params()->fromRoute('id', 0);
    if (!$id) {
        return $this->redirect()->toRoute('zfcadmin/config', array(
                'action' => 'add'
        ));
    }
    try {
        $config = $this->getConfigTable()->getConfig($id);
    }
    catch ('Exception $ex) {
        return $this->redirect()->toRoute('zfcadmin/config', array(
                'action' => 'index'
        ));
    }
    $form  = new ConfigForm();
    $form->bind($config);
    $form->get('submit')->setAttribute('value', 'Edit');
    $request = $this->getRequest();
    if ($request->isPost()) {
        $form->setInputFilter($config->getInputFilter());
        $form->setData($request->getPost());
        if ($form->isValid()) {
            $this->getConfigTable()->saveConfig($form->getData());
            return $this->redirect()->toRoute('zfcadmin/config');
        }
    }
    return array(
            'id' => $id,
            'form' => $form,
    );
}
public function getConfigTable()
{
    if (!$this->configTable) {
        $sm = $this->getServiceLocator();
        $this->configTable = $sm->get('Config'Model'ConfigTable');
    }
    return $this->configTable;
}
public function getSMTPConfigTable()
{
    $pr=$this->getConfigTable()->fetchAll();
    return $pr;
}
    }

在另一个模块类我怎么做?我已经尝试了大多数方法,但我失败了,请帮助我。(抱歉我的英语不好)我需要$temp=new ConfigController();temp -> getSMTPConfigTable ();

我首先想检查一下我是否理解了你的问题。阅读问题的名称和内容,我假设您询问在模块的另一个控制器或类中获取ConfigTable (Model)的方法。

要理解如何在ZF2中管理对象和服务,我建议您看一下这个ZF2手册页:

http://framework.zend.com/manual/2.0/en/modules/zend.service-manager.quick-start.html

如果你注意页面的"创建ServiceLocator-aware类"一节,你会发现你可以通过服务管理器本身访问任何注册到服务管理器的对象。按照它的术语,服务管理器被注入到实现ServiceLocatorAwareInterface的每个对象中,或者仅仅具有名为setServiceLocator()的方法。

明确了这一点,ZF2的Zend'Mvc'Controller'AbstractActionController实现了这个接口,这意味着您可以在对象中获得服务管理器,因此可以在对象中注册任何其他对象。如果您看一下下面的方法(来自您的Config'Controller),这里有一个从服务管理器获取模型的好例子:

public function getConfigTable()
{
    if (!$this->configTable) {
        $sm = $this->getServiceLocator();
        $this->configTable = $sm->get('Config'Model'ConfigTable'); // <-- HERE!
    }
    return $this->configTable;
}

因此,在通过ServiceManagersetServiceLocator()(可选的getServiceLocator())方法创建的每个类中,您可以以相同的方式访问您的模型:

$sm = $this->getServiceLocator();
// or 
$sm = $this->serviceLocator; // or whatever attribute you have set it to.
$configTable = $sm->get('Config'Model'ConfigTable');

关于如何调用你的模型的fetchAll()方法,我把这个决定留给你在哪里做

这正是使用ServiceManager

的地方

你会注册你的TableGateway(或无论它是你使用的"模型表")到ServiceManager像这样:

//Module.php 
public function getServiceConfig()
{
    return array(
        'factories' => array(
            'my-model-gateway' => function($sm) {
               $gw = new MyTableGateway($sm->get('Zend'Db'Adapter'Adapter'));
               return $gw;
            }
        )
    }
}

这只是一个展示场景——它不会像这样复制粘贴——我不会让你的代码变得聪明,但你可以用这个简单的例子来知道是怎么做的。我注册了一个名为my-model-gateway的服务,该服务创建了一个名为MyTableGateway的类。这个类将通过构造器注入注入默认的Zend'Db'Adapter'Adapter

使用这个,你可以从任意模块的任意控制器访问这个服务,像这样:

$gw = $this->getServiceLocator()->get('my-model-gateway');

您将需要一个构造函数。试试这个:

class ConfigController {
   function __construct() {
   }
}

即使它是空的,您也将获得类的实例,然后您将能够访问所有参数。也许您还需要为类的一般设置创建一个init