我使用路径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
我在我的应用程序/ 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中有autoloaderNamespaces.Mylib = "Mylib_"
我错了什么?
答案 0 :(得分:1)
这里有一些问题:
您没有在应用中调用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类之前运行引导程序中的_initDb()方法就可以了,这应该没问题。