Friday, June 7, 2013

Re: Can a single cakephp app connect to multiple databases?

you can use in /app/config/database.php a db switch like this:

class DATABASE_CONFIG
{

    var $default = array('driver' => 'mysql',
        'connect' => 'mysql_connect',
        'host' => 'localhost',
        'login' => 'root',
        'password' => '',
        'database' => 'db1',
        'prefix' => '');
 
    function __construct()
    {
        @session_start();
        if (isset($_SESSION['db_name'])){
            $db_name = $_SESSION['db_name'];
        }
        else{
            $db_name = 'db1';
        }
       
        $this->default = array('driver' => 'mysql',
                            'connect' => 'mysql_connect',
                            'host' => 'localhost',
                            'login' => 'root',
                            'password' => '',
                            'database' => $db_name,
                            'prefix' => '');
        // print_r($this->default);
    }

} /* end class*/


you can use session component from cake to store the database name selected like:
           $session = new CakeSession();
$session->read("db_name")
           if($session->read("db_name") ....



On Friday, June 7, 2013 3:54:02 AM UTC+2, Sam wrote:
Can a single cakephp app connect to multiple databases? When I look at Config/database.php, it seems like only 1 database can be connected. Is this a strict limitation of cakephp?

I need to connect to a database written by a 3rd-party. Then, I also need to connect to my own database. Both are MySql databases.


On Friday, June 7, 2013 3:54:02 AM UTC+2, Sam wrote:
Can a single cakephp app connect to multiple databases? When I look at Config/database.php, it seems like only 1 database can be connected. Is this a strict limitation of cakephp?

I need to connect to a database written by a 3rd-party. Then, I also need to connect to my own database. Both are MySql databases.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: