从ACL插件访问数据库

时间:2012-05-21 09:21:44

标签: database zend-framework plugins acl

我想从ACL插件中的数据库加载资源

我这样做

 class My_ACL extends Zend_Acl {

protected $_role_id;
protected $_userResource;

public function __construct() {

    try {
        $db = Zend_Db_Table::getDefaultAdapter();
        $stmt = $db->query("CALL getUserPrivileges(?)", 998877445);

        //Returns an array containing all of the result set rows  
        $rows = $stmt->fetchAll();

        $stmt->closeCursor();
        print_r($rows);

        return $rows;
    } catch (Exception $e) {
        echo 'error ' . $e;
    }
}

但这不起作用,因为白页被渲染,没有任何东西被打印出来!

1 个答案:

答案 0 :(得分:1)

我发现了问题。问题是在初始化默认适配器之前调用默认数据适配器,技巧是我必须在引导程序中获取数据适配器并将其传递给插件,所以我这样做

在bootstrap文件中

protected function _initPlugins() {
    $this->bootstrap('db');
    $db = $this->getResource('db');
    $front = Zend_Controller_Front::getInstance();
    $front->registerPlugin(new Application_Plugin_Acl($db));
}

在Application_Plugin_Acl中,我这样做

class Application_Plugin_Acl extends Zend_Controller_Plugin_Abstract {

 public function __construct($db) {
    $this->_acl = new My_ACL($db);
}
}

这是my_ACL

 class My_ACL extends Zend_Acl {


public function __construct($db) {

     try {

      $stmt = $db->query("CALL getUserPrivileges(?)", 998877445);

      //Returns an array containing all of the result set rows
      $rows = $stmt->fetchAll();

      $stmt->closeCursor();
      print_r($rows);

      return $rows;
      } catch (Exception $e) {
      echo 'error ' . $e;
      }

}