Variable database setting Cakephp - cakephp

I'm searching a way to create a variable database in Cakephp.
I'm creating a website in wich the database setting (user, pass, db) depends of the user who logg's in. The database settings come from another database.
Thanks in advance,
AƤron
-- ANSWER
Thanks for your quick reply.
I know how to switch between databases, but the database settings have to be variable.
Example
$db = $this->DB_SET['DB_SET']['db']; //from model DB_SET the database
$db_login= $this->DB_SET['DB_SET']['login'];
$db_host= $this->DB_SET['DB_SET']['host'];
$db_pass= $this->DB_SET['DB_SET']['pass'];
var $db2 = array(
'driver' => 'mysql',
'persistent' => false,
'host' => $db_host,
'login' => $db_login,
'password' => $db_pass,
'database' => $db,
'prefix' => '',
'encoding' => 'UTF8',
'port' => '',
);

If you're looking to create database connections on the fly, you may want to use ConnectionManager::create().
// get logged in user's id and create a connection for them
$id = $this->Auth->user('id');
ConnectionManager::create("user_$id_db", array(
'driver' => 'mysql',
'persistent' => false,
'host' => $db_host,
'login' => $db_login,
'password' => $db_pass,
'database' => $db,
'prefix' => '',
'encoding' => 'UTF8',
'port' => '',
));
Then set your models to use it when you're ready.
// get logged in user's id and use their connection
$id = $this->Auth->user('id');
$this->Model->setDataSource("user_$id_db");

Related

How to access remote database from laravel 4.2

I am currently do some development in a early developed system developed from laravel 4.2.
I run it in my local machine. At the same time now I have to access to a another System's database.
For testing purposes i have created that db in my local server. I don't have direct access to that db in real situation. Only i have a view. So please tell me how should i configure my system to achieve that task.
First you have to add a separate database connection to your other system's database, so in your app/config/database.php you can have this:
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'forge',
'username' => 'forge',
'password' => '123456',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'mysql2' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'forge2',
'username' => 'forge2',
'password' => '123456',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
In order to perform query to the other database you have to do this:
$users = DB::connection('mysql2')->select('select * from users where id = ?', array(1));
You may see this code in the Laravel's documentation: https://laravel.com/docs/4.2/database#accessing-connections

DRUPAL 8 - module Views database connector

I need for my drupal 8 installation to connect a second database for a view.
I install the module views_database_connector (january 2015 release)
I connect with the setting.
but nothing works, i don't see my database with view.
my settings...
$databases['default']['default'] = array (
'database' => 'first base',
'username' => 'first base',
'password' => 'xxxxx',
'prefix' => '',
'host' => 'localhost',
'port' => '3306',
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
'driver' => 'mysql',
);
$databases['external']['default'] = array (
'database' => 'second_base',
'username' => 'second_base',
'password' => 'yyyyyy',
'prefix' => '',
'host' => 'localhost',
'port' => '3306',
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
'driver' => 'mysql',
);
Does this module need a patch, and how to apply ??
For this you can create multiple database connection in setting file :
use \Drupal\Core\Database\Database::setActiveConnection('external'); to get current active connection.

Laravel - Set up dynamic database connection on Multi tenant application

Within my app I have different businesses and those have a number of users.
For example:
Business A has UserA, UserB and UserC
Business B has UserD and UserE
And so on..
Each business has its own separate database, so Users A, B and C access the same database, and Users D and E access a different database (Every tenant database is identical in structure, the only thing that differs is the data).
There is a main database that has this information for each user, so I know which database a user belongs.
'main' => array(
'driver' => 'mysql',
'host' => 'hostname',
'database' => 'main_database',
'username' => 'username',
'password' => 'password',
'prefix' => '',
),
'tenant' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => DYNAMIC_DATABASE_NAME_GOES_HERE,
'username' => 'username',
'password' => 'password',
'prefix' => '',
),
I need to find a way to do the following in Laravel:
User signs in to the app
After login I get the user database identifier/database name using the main database connection
Set that specific database name within a connection called "tenant"
App uses that tenant connection to load the data of that specific
user/business
How can I accomplish this in Laravel 5.4?
This link has a very good example of what you are looking for.
1) Setup two connections in your database config.
'main' => array(
'driver' => 'mysql',
'host' => 'hostname',
'database' => 'database',
'username' => 'username',
'password' => 'password',
'prefix' => '',
),
'tenant' => array(
'driver' => 'mysql',
'host' => '',
'database' => '',
'username' => '',
'password' => '',
'prefix' => '',
)
2) Then to switch the DB, put the following code in your filters or middleware. Considering you have Tenant model for database connection info.
$tenant = Tenant::whereSubDomain($subdomain)->first();
Config::set('database.connections.tenant.host', $tenant ->host);
Config::set('database.connections.tenant.username', $tenant ->username);
Config::set('database.connections.tenant.password', $tenant ->password);
Config::set('database.connections.tenant.database', $tenant ->database);
//If you want to use query builder without having to specify the connection
Config::set('database.default', 'tenant');
\DB::purge('tenant');
\DB::reconnect('tenant');
dd(\DB::connection('tenant'));
3) Specify following in your models
//On models dealing with the main DB
protected $connection = 'main';
//On models dealing with tenant DBs
protected $connection = 'tenant';

how to connect to multiple databases in drupal in order to display the content in a table

i am using drupal in order to create a website on my localhost machine.
i have a mysql database that i want to connect it to the drupal website and display its data in a table.
this is what i did in settings.php file
//connect to external database
$databases['sitesdb']['default'] = array (
'database' => 'sitesdb',
'username' => 'xxxxxxx',
'password' => 'xxxxxxxx',
'prefix' => '',
'host' => 'localhost',
'port' => '3306',
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
'driver' => 'mysql',
);
and in the drupal i download the Views Database Connector (VDC) module in order to connect to the external database.
but it did not work .
second method i used views and created content types that are the fields table of the external databases.
but i do not know how to continue to display the data.
I was using migrate module and multiple DB configuration looked like:
$databases = array (
'default' =>
array (
'default' =>
array (
'database' => 'main_db',
'username' => 'XXX',
'password' => 'XXX',
'host' => 'localhost',
'port' => '',
'driver' => 'mysql',
'prefix' => '',
),
),
'legacy' =>
array (
'default' =>
array (
'database' => 'additional_db',
'username' => 'XXX',
'password' => 'XXX',
'host' => 'XXX',
'port' => '',
'driver' => 'mysql',
'prefix' => '',
),
),
);

Connect multiple database in one controller cakephp 2

Hello I am new to cakephp 2. I want to know, how to connect to database in one controller and loop for each. Please help me with details.
I have already set the following in the database config:
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'db_one',
'prefix' => '',
//'encoding' => 'utf8',
);
public $database2 = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'db_announcement',
'prefix' => '',
//'encoding' => 'utf8',
);
You should research bit more in google before asking question here.
Anyways, CakePHP takes default database connection which is there in database.php in core folder, so all your queries which you execute in the database which is defined in default vairable in database.php and if you want to connect multiple database than you can try something like this.
In your database configuration file:
public $database2 = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '*******',
'database' => '*****',
'prefix' => '',
'encoding' => 'utf8',
);
And in your controller you can have connection in this way:
$db = ConnectionManager::getDataSource('user');
$list = $db->rawQuery('your query'); or customized CakePHP queries can work here too
I hope you will get your answer from this.
Thanks
$i=0;
$total=$list->fetchAll(PDO::FETCH_ASSOC);
instead of getDataSource method I would suggest to use $this->ModelName->setDataSource('mongo');
E.g If you want to fetch the data from User model of 'mongo' database source , which you have mentioned in database.php, then to set that it would look like.
$this->User->setDataSource('mongo')
in database.php you are having following code
public $mongo = array(
'datasource' => 'Mongodb.MongodbSource',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => 'root',
'database' => 'mongo_data',
'prefix' => '',
'encoding' => 'utf8',
);

Resources