Phalcon - Switching Databases


Advertisements


We have used a MySQL database in our application. If we wanted to change the database software midstream, it would not be too hard, as long as we have the same data structure in our new database.

PostgreSQL

Configure the web application which will connect to PostgreSQL database.

This can be achieved using the following code. The services will include Phalcon\Db\Adapter\Pdo\Postgresql

use Phalcon\Db\Adapter\Pdo\Postgresql;  

$config = [ 
   'host'     => 'localhost', 
   'dbname'   => 'blog_tutorial', 
   'port'     => 5432, 
   'username' => 'root', 
   'password' => '' 
];  

$connection = new Postgresql($config); 

SQLite

For implementing SQLite connection, the configuration should be extended with Phalcon\Db\Adapter\Pdo\Sqlite abstract class.

<?php  
 
use Phalcon\Db\Adapter\Pdo\Sqlite;  

$connection = new Sqlite(['dbname' => '/tmp/blog_tutorial.sqlite']); 

Oracle

For implementing Oracle database connection in Phalcon, the configuration should be extended with Phalcon\Db\Adapter\Pdo\Oracle abstract class.

<?php  

use Phalcon\Db\Adapter\Pdo\Oracle;  

$config = array ( 
   "dbname" => "//localhost/blog_tutorial", 
   "username" => "root", 
   "password" => "" 
); 

$connection = new Phalcon\Db\Adapter\Pdo\Oracle($config); 


Advertisements