如何在我自己的ZF库中连接到mysql?

| 我创建了自己的带路径的库:
library/Mylib
。我在
library/Mylib/
中有一个名为
Acl.php
的课程:
    class Mylib_Acl extends Zend_Acl{
        public $_db;

        public function init(){
            $this->_db = new Zend_Db_Adapter_Pdo_Mysql(
                array(
                    \'host\' => \'localhost\',
                    \'username\' => \'username\',
                    \'password\' => \'password\',
                    \'dbname\' => \'mydb\'
                )
            );
            Zend_Db_Table_Abstract::setDefaultAdapter($this->_db);
        }

public function __construct(){
        self::initRole();

    }

        public function initRole(){
            Zend_Debug::dump($this->_db);
            Zend_Debug::dump(Zend_Registry::get(\'db\'));
            die;
            $sql = \'SELECT * FROM some_table\';
                $result = $this->_db->query($sql)->fetchAll();

        }
Zend_Debug::dump($this->_db)
给我:
NULL
Zend_Debug::dump(Zend_Registry::get(\'db\'))
给我这个错误:
Fatal error: Uncaught exception \'Zend_Exception\' with message \'No entry is registered for key \'db\'\' in /home/truong/public_html/test2/library/Zend/Registry.php:147 Stack trace: #0 /home/truong/public_html/test2/library/Fxml/Acl.php(19): Zend_Registry::get(\'db\') #1 /home/truong/public_html/test2/library/Fxml/Acl.php(62): Fxml_Acl->initRole() #2 /home/truong/public_html/test2/application/Bootstrap.php(25): Fxml_Acl->__construct() #3 /home/truong/public_html/test2/library/Zend/Application/Bootstrap/BootstrapAbstract.php(666): Bootstrap->_initAutoload() #4 /home/truong/public_html/test2/library/Zend/Application/Bootstrap/BootstrapAbstract.php(619): Zend_Application_Bootstrap_BootstrapAbstract->_executeResource(\'autoload\') #5 /home/truong/public_html/test2/library/Zend/Application/Bootstrap/BootstrapAbstract.php(583): Zend_Application_Bootstrap_BootstrapAbstract->_bootstrap(NULL) #6 /home/truong/public_html/test2/library/Zend/Application.php(355): Zend_Application_Bootstrap_BootstrapAbstract->bootstrap(NULL) #7 /home/truong/pu in /home/truong/public_html/test2/library/Zend/Registry.php on line 147
我确实在我的application / Bootstrap.php中注册了\'db \'键:
public function _initDb(){
         $db = new Zend_Db_Adapter_Pdo_Mysql(
            array(
                \'host\' => \'localhost\',
                \'username\' => \'username\',
                \'password\' => \'password\',
                \'dbname\' => \'mydb\'
            )
        );
        Zend_Db_Table_Abstract::setDefaultAdapter($db);

        Zend_Registry::set(\'db\', $db);
    }
我的确在application.ini中有:9ѭ 我怎么了     
已邀请:
        这里有一些问题: 永远不会调用Acl类中的init()方法(至少在您的示例中不会) init()方法正在创建另一个数据库连接-最好重用您在引导程序中创建的数据库连接 当它不是静态方法时,您将静态调用initRole() 您尚未在调用Acl类的应用程序中包含该部分,这可能是导致错误的原因。例如。如果您在设置数据库资源之前在引导程序中实例化该类,则可以解释您所看到的错误。 通过将其更改为以下内容,可以大大简化您的课程:
class Mylib_Acl extends Zend_Acl{
    public $_db;

    public function __construct(){
        $this->init();
        $this->initRole();
    }

    public function init(){
        $this->_db = Zend_Registry::get(\'db\');
    }

    [etc.]
}
只要在使用Acl类之前运行了bootstrap中的_initDb()方法,就可以了。     

要回复问题请先登录注册