如何在Controller \ Plugin \ MyPlugins ZF2中访问Zend \ db \ Adapter?

时间:2014-07-21 13:09:23

标签: zend-framework2

如何从我的控制器插件Zend\db\Adapter中访问Controller\Plugin\MyPlugins

我想执行

$this->getServiceLocator()
     ->getServiceLocator()
     ->get('Zend\Db\Adapter\Adapter')
     ->query("Select * from ABC ") 

2 个答案:

答案 0 :(得分:2)

在插件中,您可以使用以下命令访问控制器(提供扩展Zend\Mvc\Controller\Plugin\AbstractPlugin),从而访问服务管理器:

$this->getController()->getServiceLocator();

然而,访问服务管理器以从 中获取插件依赖项(适配器)是非常糟糕的做法。

很多更好的解决方案是注入'使用服务工厂的适配器。

use MyModule\Mvc\Controller\Plugin\MyPlugin;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\FactoryInterface;

class MyPluginFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $pluginManager)
    {
        $serviceManager = $pluginManager->getServiceLocator();

        return new MyPlugin(
            $serviceManager->get('My/Db/Adapter') // inject db adapter
        );
    }
}

修改插件的__construct以允许适配器进入

class MyPlugin extends AbstractPlugin
{
    protected $dbAdapter;

    public function __construct(Adapter $dbAdapter)
    {
        $this->dbAdapter = $dbAdapter;
    }
    //...
}

最后在Module.php(或module.config.php

中将其注册为控制器插件
// Module.php
public function getControllerPluginConfig()
{
   return array(
     'factories' => array(
       'MyPlugin' => 'MyModule\Mvc\Controller\Plugin\MyPluginFactory'
     ),
   );
}

答案 1 :(得分:0)

这是我的代码片段

在控制器路径“modules / Admin / Controller / AreaController.php”

 public function arealistAction() {

    $Search = new Plugin\SearchBox();  //calling the searchbox class
    $Search->setButtons('Operator', 'query', 'Select id,name as text from Operator where Status=1 ');
    //passing parameter for searchbox
    $box = $Search->renderSearchBox();  

  }

SearchBox在“modules / Admin / Controller / Plugin / SearchBox.php”下面的代码

namespace Admin\Controller\Plugin;

 class SearchBox extends \Zend\Mvc\Controller\Plugin\AbstractPlugin {

  protected $RawSearch = array();

 //Get the user data 
  public function setButtons($label, $opt, $dbcols, $values = "") {
    $this->$RawSearch = array(
      'label' => $label,
      'opt' => $opt,
      'values' => $values,
      'dbcols' => $dbcols,
    );
  }

 //Process the search value 
  public function ProcessSearchValue($val) {

    switch ($val['opt']) {
      case 'date':
      case 'text':
        $vHtml = '<input type="text" name="' . $val['dbcols'] . '" id="' . $val['dbcols'] . '">';
        break;
      case 'query':
        $dbadpater = $this->getController()->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
        $Statement = $dbadpater->query($val['values']);
        $results = $Statement->execute();

        $vHtml = '<select name="' . $val['dbcols'] . '" id="' . $val['dbcols'] . '">';
        if (!empty($results)) {
          foreach ($results as $rkey => $rval) {
            $vHtml .='<option value="' . $rval['id'] . '">' . $rval['text'] . '</option>';
          }
        }
        $vHtml .='</select>';
        break;
    }
    return $vHtml;
  }


  public function ProcessOptValue($val) {
    $oHtml = '<select name="' . $val['dbcols'] . '" id="' . $val['dbcols'] . '">';
    $oHtml .='<option value="eq">Equal</option>';
    switch ($val['opt']) {
      case 'date':

        break;
      case 'text':
        $oHtml .='<option value="cnt">Contain</option>';
        $oHtml .='<option value="ncnt">Not Contain</option>';
        break;
      case 'query':
        $oHtml .='<option value="lth">Less Than</option>';
        $oHtml .='<option value="gth">Greater Than</option>';
        $oHtml .='<option value="leq">Less than Equal</option>';
        $oHtml .='<option value="geq">Greater than Equal</option>';
        break;
    }
    $oHtml .= '</select>';

    return $oHtml;
  }

  public function renderSearchBox() {
    if (!empty($this->RawSearch)) {
      $Sbox = '<table cellspacing="0px" cellpadding="0px">';
      foreach ($this->RawSearch as $key => $val) {
        $Sbox.= '<tr>'
            . '<td><label>'.$val['label'].'</label></td>'
            . '<td>'.$this->ProcessOptValue($val).'</td>'
            . '<td>'.$this->ProcessSearchValue($val).'</td>'
            . '</tr>';
      }
      $Sbox.='</table>';
    }
    return $Sbox;
  }

 }

现在我执行语句“$ box = $ Search-&gt; renderSearchBox();”它给了我错误。 我无法在插件中执行查询。