CakePHP select database on login

164 views Asked by At

I have two databases and want to select one database at the time of login. I have userid: user1 in both databases and I have userid, password and database dropdown option to select database in login form, how I can achive this, by this way or some other way?

In database.php

class DATABASE_CONFIG {

public $default = array(
    'datasource' => 'Database/Mysql',
    'persistent' => false,
    'host' => 'localhost',
    'login' => 'root',
    'password' => '',
    'database' => 'abc',
    'prefix' => '',
    //'encoding' => 'utf8',
);

public $second = array(
    'datasource' => 'Database/Mysql',
    'persistent' => false,
    'host' => 'localhost',
    'login' => 'root',
    'password' => '',
    'database' => 'xyz',
    'prefix' => '',
    //'encoding' => 'utf8',
);
1

There are 1 answers

1
Waldemar Neto On

You can override the Model constructor setting the datasource as you need. Like this

class AppModel extends Model
{
    public function __construct($id = false, $table = null, $ds = null)
    {
        parent::__construct($id, $table, $connection_name);
        $this->useDbConfig = $connection_name;
    }
}

Ask me if you need more information