简体   繁体   中英

Zend Framework Quickstart Application - Database connection parameters. How are they passed?

I am in the process of learning Zend Framework and I am trying to understand how to set up connections to databases.

I understand a few way to do it, but I would like to understand how the quickstart application in the zend tutorial passes the database parameters in the application.ini file into the code. The page in question is here: http://framework.zend.com/manual/en/learning.quickstart.create-model.html .

I can see no explicit call to get the parameters and I am assuming it is something to do with how the following class works.

class Application_Model_DbTable_Guestbook extends Zend_Db_Table_Abstract

Could someone tell me how this application is getting the details for the database from the application.ini file?

Many thanks

The DB adapter is set up using the Zend_Application_Resource_Db plugin.

The bootstrap calls the plugin with the settings from the db section of the ini file of the application. It creates an adapter, and then sets it as the default adapter for Zend_Db_Table, using the static setDefaultAdapter method.

This is then used as the adapter for all tables (which extend Zend_Db_Table_Abstract), unless you pass a different adapter to their constructor.

Here's the documentation for resource plugins .

The short answer is, it happens during the bootstrap process. The long answer is, Zend_Config objects are created for each of your zpplication.ini sections and the Zend_Config created from your application.ini is used to configure the DBAdapter specific to your setup, which the Zend_DB_Table_Abstract uses for querying its backing store. The long long answer is... read the code and documentation...

To encapsulate the responses as I understand for the benefit of those who may be stuck on the same issue:

Anything with the prefix resources.db will be loaded by default by the Zend_Application_Bootstrap class as a resource relating to Zend_Db.

The following code creates a class which names the table in the database.

class Application_Model_DbTable_Guestbook extends Zend_Db_Table_Abstract
{
    protected $_name = 'guestbook';
}

The following code creates an instance of the above which is how the application 'knows' there is a Zend_Db_Table type object:

class Application_Model_GuestbookMapper
{

    protected $_dbTable;

    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable(); // this line creates an object of type 
                                       // 'Application_Model_DbTable_Guestbook'...
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Application_Model_DbTable_Guestbook'); // ...from this line
        }
        return $this->_dbTable;
    }

And.. Voila!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM