Using multiple database connections in CakePHP model

Hello Readers,

Till now we may develop our application with single database . Is their any ways to integrate our application with multiple databases ? . Yes we have.

There are methods to use multiple databases for development and for production without changing anything but rewriting a function in DATABASE_CONFIG class.

Just open your database.php file inside config folder and search this class “DATABASE_CONFIG”.

Here you can add multiple database configuration.

  1. class DATABASE_CONFIG {
  2. public $default = array(
  3. ‘datasource’ => ‘Database/Mysql’,
  4. ‘persistent’ => false,
  5. ‘host’ => ‘localhost’,
  6. ‘login’ => ‘username’,
  7. ‘password’ => ‘password’,
  8. ‘database’ => ‘database1’,
  9. ‘prefix’ => ,
  10. //’encoding’ => ‘utf8’,
  11. );
  12. public $test1 = array(
  13. ‘datasource’ => ‘Database/Mysql’,
  14. ‘persistent’ => false,
  15. ‘host’ => ‘localhost’,
  16. ‘login’ => ‘username’,
  17. ‘password’ => ‘password’,
  18. ‘database’ => ‘database2’,
  19. Prefix PHP => ,
  20. //’encoding’ => ‘utf8’,
  21. );
  22. public $test2 = array(
  23. ‘datasource’ => ‘Database/Mysql’,
  24. ‘persistent’ => false,
  25. ‘host’ => ‘localhost’,
  26. ‘login’ => ‘username’,
  27. ‘password’ => ‘password’,
  28. ‘database’ => ‘database3’,
  29. ‘prefix’ => ,
  30. //’encoding’ => ‘utf8’,
  31. );
  32. }

The default database is “public $default” , if you want to use database2 you have to initialize “public $test 1” in your Model.

Open your UserModel.php

For further queries or PHP Programming Questions and Answers you can Visit to our  PHP Developer Forum

Advertisements
Using multiple database connections in CakePHP model

3 thoughts on “Using multiple database connections in CakePHP model

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s