Showing posts with label DATABASE CONFIG. Show all posts
Showing posts with label DATABASE CONFIG. Show all posts

Tuesday, 30 June 2015

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