Zend Framework2 Another way to implement multi-database configuration

Source: Internet
Author: User
Tags zend

Compared to the first one I ever made in Zend Framework2 to implement multi-database configuration use: http://blog.csdn.net/a437629292/article/details/41121455; I prefer this method!

Don't worry, listen to me slowly.

1. Problem: The first method (see above connection), is not to instantiate a model, all to instantiate a database adapter, this is not a bit of a waste of resources?

So, I have been dissatisfied with this implementation, has been looking for how to be able to a request (regardless of the number of model calls), even from the start of the project, only the database adapter is instantiated once.

2. Idea: Pubilc/index.php is the only entry file, which means that all requests need to go through index.php, then there is a little inspiration, is not I in the index.php to instantiate a database adapter array,

Then according to the configuration or parameters, to choose which database adapter to instantiate?

3. Practice: Now that you have this idea, hurry to practice one:

Add the following code to the index.php:

Instantiate all the 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; $registry =zend_registry:: GetInstance (); $configArray [$databaseName] = $config; $registry->set (' Configarray ', $configArray);//Configuration Database $db= Zend_db::factory ($config->db); Instantiate an appropriate 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::setdefaultadapter ($DB);}} $registry->set (' dbadapters ', $dbAdapters);
In this way, all the metabase nodes in the Serverconfig.ini are instantiated into the database adapter and loaded into the array, which we can use.

What I do now is to set the default database adapter, and 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

In this way, you can easily implement the database by setting the default parameter instead of using it! OY ...

International practice: Welcome to shoot Bricks!

Zend Framework2 Another way to implement multi-database configuration

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.