Search code examples
zend-frameworkzend-db

how to connect zend_Db in one file


when i want to connect database in zend framework with zend_Db, in each controller or model must write this code:

$params = array(
'host'           => '127.0.0.1',
'username'       => 'webuser',
'password'       => 'xxxxxxxx',
'dbname'         => 'test',
'charset'        => 'utf8'
);

but when use Doctrine it's enough to write this code in application.ini

doctrine.dsn = "mysql://user:pass@localhost/dbase"

how i can use zend_Db with out set connection setting in each file?


Solution

  • open

    application/configs/application.ini
    

    add following lines

    resources.db.adapter = PDO_MYSQL
    resources.db.isDefaultAdapter = true
    resources.db.params.host = localhost
    resources.db.params.username = root
    resources.db.params.password = 
    resources.db.params.dbname = foo
    

    and then inside your Bootstrap.php

    do

    public function _initSetup()
    {
    $this->bootstrap('db');
    }
    

    and then whereever or whenever you need db instance simply do

    $db = Zend_Db_Table::getDefaultAdapter();