I am new to Zend Framework.
I try to access MySQL database by
$db = new Zend_Db_Adapter_Pdo_Mysql(
[
'host' => 'localhost',
'username' => 'user',
'password' => 'pass',
'dbname' => 'database'
]
);
$query = 'SELECT * FROM table_name';
$this->view->rec = $this->db->fetchAll($query);
On every controller to access database fine. (I want Single Config.)
So, i try via Bootstrap config,
$this->bootstrap('db');
switch (APPLICATION_ENV) {
case 'development' :
// this allows you to profile your queries through the firebug console
$profiler = new Zend_Db_Profiler_Firebug('System Queries');
$profiler->setEnabled(true);
$this->getPluginResource('db')->getDbAdapter()->setProfiler($profiler);
break;
case 'production' :
// if you use meta caching in production, which you should :)
// Zend_Db_Table_Abstract::setDefaultMetadataCache($this->_cache);
break;
}
How to access this Instance to retrieve data from "table" ?
Or any other solutions are there ?
Thanks in Advance !
Try to setup the default adapter via Adapter Factory
$db = Zend_Db::factory('Pdo_Mysql', array(
'host' => BASEHOST,
'username' => BASEUSR,
'password' => BASEPASS,
'dbname' => BASE
));
Zend_Db_Table::setDefaultAdapter($db);
And now you can build a model for your table
<?php
class YourTable extends Zend_Db_Table_Abstract{
protected $_name = 'table' //your table;
}
Finally fetch the data
<?php
$model = new YourTable();
$this->view->red = $model->fetchAll();
The advantage of this approach is now you can decouple your queries and now you can integrate your custom logic right on your model.