Yii 1.1: Encapsulate db and module configurations


This method was inspirated on the excellent text about different environments available at http://www.yiiframework.com/doc/cookbook/32/


Suppose you have a project with an increasing number of modules or configurations. So, not rarely you have to change your config/main.php file.

This is troublesome when you work on an environment with development/test/production scenarios. As a simple developer, you must NOT have access to production DB passwords and, consequently, you must not have access to config/main.php at production (it contains the db password).

You will annoy your DBAs every time you must to post a change at config/main.php, because you'll have to ask them to change the password from dev to production password. This leads to very potencial problems:

  • You can forget this and break your system (and every module within it);
  • To have not a DBA available and your system update will have to wait.


To overcome this problem, I suggest to make use of CMap::mergeArray function to encapsulate db and module configurations in specific files. Making this, you can have these properties protected from careless using (you can still apply some other security actions on them).

Let's see how to do this:

Create a db.php file at config directory. Then, specify your db configurations:


return array(

OPTIONALLY, you can create a modules.php file to encapsulate modules properties. Create it at 'config' dir too.


return array(
    'modules' => array(     
        'module1' => array(             
        'module2' => array(             

Now, the key to this method.

Once you have the pre-config files above, you can change config/main.php as below:

config/main.php (at the very beginning):

$pre_config = 

This will load db and modules configurations to the variable $pre_config

So, we will merge this configurations with the others at main.php (after the last code block):

return CMap::mergeArray($pre_config, array(
    'name'=>'My Project',

Remember to NOT redefine db or module configurations at main.php, instead you will overwrite the previous configurations.

Now you have three configuration files: main.php, db.php and modules.php (I thought about to create a components.php too, but it sounded as unnecessary)

*ps: English corrections are welcome. I am not an expert =)

Total 3 comments

#2700 report it
zaccaria at 2011/01/31 02:01am
svn application

This method is very confortable if you are managing update with svn (or any other revision control system).

The database configuration is a part of config file that cannot reside under svn control, because each developer has his own dbsystem and the production copy too has his own config.

Using this method we can create a small, separed config file for db only, that we will 'ignore' in svn, that allows to keep the main configuration file under revision control and at a time avoid to override the db configuration.

#1155 report it
webid at 2009/11/20 07:49pm

Thank you Jerry, your method worked great...

example /protected/config/db.php:

return array(

in /protected/config/main.php:

#1168 report it
jerry2801 at 2009/11/18 09:31pm
how about this method?



Leave a comment

Please to leave your comment.

Write new article
  • Written by: ricardograna
  • Updated by: Yang He
  • Category: Tutorials
  • Yii Version: 1.1
  • Votes: +7 / -1
  • Viewed: 13,730 times
  • Created on: Oct 20, 2009
  • Last updated: Jun 30, 2012
  • Tags: module