Zend framework2 is another method for implementing multi-database configuration, zendframework2

Source: Internet
Author: User

Zend framework2 is another method for implementing multi-database configuration, zendframework2

Reprinted, please indicate the source and respect the original: http://blog.csdn.net/a4376292/article/details/41485611. thank you.

Compared with the first method I have used to implement multi-database configuration in zend framework2: http://blog.csdn.net/a437629292/article/details/43791455? I can use this method more easily!

Don't worry. Let me hear it.

1. Question: The first method (see the above connection) is to instantiate a database adapter for every model instantiation. Is this a waste of resources?

Therefore, I have been dissatisfied with this implementation method. I have been searching for how to make a request (no matter how many model calls are included), or even instantiate the database adapter only once after the project is started.

2. idea: pubilc/index. php is the only entry file, that is, all requests must go through the index. php, so there is a little inspiration, is it in index. instantiate a database adapter array in php,

Then, based on the configuration or parameters, select which database adapter to instantiate?

3. Practice: Now that you have this idea, please try it out:

Add the following code to index. php:

// Instantiate all database adapters $ configs = new Zend_Config_Ini (APPLICATION_PATH. '/configs/serverConfig. ini '); $ dbAdapters = array (); // $ configArray = array (); $ registry = Zend_Registry: getInstance (); foreach ($ configs as $ config) {$ databaseName = $ config-> db-> params-> dbname; $ configArray [$ databaseName] = $ config; $ registry-> set ('configarray', $ configArray ); // configure database $ db = Zend_Db: factory ($ config-> db); // instantiate a suitable database adapter $ db-> query ("set names utf8 "); $ dbAdapters [$ databaseName] = $ db; $ dbAdapters [$ databaseName] = Zend_Db: factory ($ config-> db-> adapter, $ config-> db-> params-> toArray (); if ($ config-> db-> params-> default) = 1) {Zend_Db_Table :: setdefaadapter adapter ($ db) ;}}$ registry-> set ('dbadapters ', $ dbAdapters );
In this way, all the configuration database nodes in serverConfig. ini are turned into database adapters and loaded into arrays. In the array, we can use them.

What I implement now is to set the default database Adapter. The code in serverConfig. ini is as follows:

[proManager]db.adapter = PDO_MYSQLdb.params.host = 127.0.0.1:3306db.params.username = rootdb.params.password = rootdb.params.dbname = proManagerdb.params.default = 0[proManager1]db.adapter = PDO_MYSQLdb.params.host = 127.0.0.1:3306db.params.username = rootdb.params.password = rootdb.params.dbname = proManager1db.params.default = 1

By setting the default parameter, you can easily change the database usage! OY...

International practice: Welcome!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.