Tuesday, July 4, 2017

Yii2: Multiple Database with MySQL & PosgreSQL combined


I created this Blog post to help individuals how to combine two different database in one Yii2 application. I searched almost a month figuring out how to combine database but no luck. After spending a couple of days on debugging and test trials, I was able to combine PostgreSQL and Mysql on one application. This tutorial is for Linux Based users who wants to combine MySQL and PosgreSQL in one Yii2 application. Im using pgAdmin for PostgreSQL and phpmyadmin for MySQL. I also used advance template for this tutorial.


1. Install LEMP. Depending on your operating system, you can use xamp or wamp but I prefer installing LEMP stack because of efficiency, integrity and professionalism. 

2. Check php module. Check to see if mysql and pgsql module are already installed on the system. If not, then you need to install them. If pgsql module is not installed on your system, it will get an error. 


php -m | less

sample of mysql and pgsql module
Sample error if pgsql is not installed in your system




2. I'm using 'coms' database for testing purpose and I created table called 'test'. I generated CRUD using gii. Then I populated some data.

mysql database
generated crud using gii
3. I created 'test' database in pgAdmin which uses PostgreSQL. I also created 'alderson' table and populated some data. 


My pgAdmin
4. Now these part were all related to Yii2 framework. first configure 'common/config/main-local.php' .



5. Create a model called 'Alderson.php'.



6. In view/test/_form.php, to test our second database, we need to incorporate the second database (postgresql) by creating dropdown menu to our name field.



7. We need to test in browser to see the changes. As you can see, we can now get the data from postgresql. 



No comments:

Post a Comment