I created my own library with path: library/Mylib. I have a class in library/Mylib/ named 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) give me : NULL
Zend_Debug::dump(Zend_Registry::get('db')) give me this error :
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
I did register ‘db’ key in my application/Bootstrap.php :
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);
}
And I did have : autoloaderNamespaces.Mylib = "Mylib_" in application.ini
What did I wrong?
There are a few issues here:
You’ve not included the part of your app where you call the Acl class, which may be the cause of the error. E.g. if you were instantiating the class in your bootstrap before the database resource was setup, this would explain the error you are seeing.
You could greatly simplify your class by changing it to something like this:
as long as the _initDb() method in the bootstrap is run before you use the Acl class this should then be fine.