Skip to main content

How to Use Two Database in Yii PHP Framework

Recently in one of Ireland based project client has requirement to use existing Microsoft SQL Server database and implement new feature and store data into MySQL database with Yii Framework.

First we have install and implement microsoft sql server driver in wamp server to make in localhost environment. Now we have to connect both the Database in Yii Framework. Following is steps to implement in Yii Framework :

1. In protected/config folder open main.php and add below code in component for mysql and sql server connection
'db'=>array(  // default mysql connection
 'connectionString' => 'mysql:host=localhost;port=3306;dbname=db_name',
 'emulatePrepare' => true,
 'username' => 'db_username',
 'password' => 'db_password',
 'charset' => 'utf8',
 'tablePrefix' =>'tbl_',  // if have prefix in database tables
),
'msdb'=>array( // microsoft sql server connection
 'class' => 'CDbConnection',
 'connectionString' => 'sqlsrv:Server=host_name or ip_address;Database=db_name',
 'username' => 'db_username',
 'password' => 'db_password',
 'charset' => 'GB2312',
),   

2. Now sql server connection is available in your entire application and access by CDbCommand
$company_name=Yii::app()->msdb->createCommand("SELECT TOP 1 CompanyName from Company_Details WHERE CompanyID="ezeelive")->queryRow();
if(isset($company_name) && !empty($company_name["CompanyName"])):
 echo $company_name["CompanyName"]; // it will print company name Ezeelive Technologies
else:
 echo '';
endif; 

In above example we have use msdb define database connection in component

3. Sometime we require to get the data from one table and insert into another database table, In this case you can use createCommand to write custom loop or you can Switch database connection runtime in model class
class Company extends CActiveRecord
{
 public function init()
 {
 $this->attachEventHandler('OnBeforeSave',   array($this,'switchToWriter'));
 $this->attachEventHandler('OnAfterSave',   array($this,'switchToReader'));
 }
 public function switchToWriter()
 {
 self::$db=Yii::app()->db;
 return true;
 }
 public function switchToReader()
 {
 self::$db=Yii::app()->msdb;
 return true;
 }
 //...
}    

In above example database connection will swapping means the connection will automatically change in fetching and inserting time. "switchToReader" function for fetching the data from SQL Server database and "switchToWriter" for insert/save data into MySQL database.

Please fill free to contact us on info@ezeelive.com or leave a comment for any type of development, integration and coding problem. Keep Coding...!!

Comments

Popular posts from this blog

Comparison between Prestashop and CS Cart eCommerce System

We (Ezeelive Technologies) are comparison CS Cart with another famouns eCommerce system called Prestashop. Both Prestashop and CS Cart use their own in-build Php MVC pattern. Our development team have find  Prestashop  has such good features like Frontend and Backend Speed, Product Comparison, URL Rewriting, HTML5 Image Uploader, Taxes, Stores Locator etc. Read more :  http://www.ezeelive.com/blog/comparison-prestashop-cs-cart-ecommerce-system/

Advantages of Php Web Development

Now a day PHP has turn out to be the most demanding web programming language for web development. There are several reasons why PHP Development is in great demand these days. If you would like to hire PHP developers to develop your business or not, it's a fine thought to be aware of these advantages associated with the development of PHP to understand your best options. PHP has become one of the most useful web programming platforms, as well as Linux and MySQL . The only costs associated with working with these technologies are the cost of hiring PHP Web developers . Benefits of development in PHP language: 1. It is an open source language therefore it's free. 2.  PHP development provides the best performance at an affordable price which is a great dynamic pages. 3. Coding is very easy to learn 4. PHP language syntax is similar to those of C and C++ languages. 5. Any changes to the site during or after the development can be done easily. 6. The framework and the