Yii 2.0 : 2 database connection - php

I read this question yii 2.0 multiple database connection, and use the answer of #Ali MasudianPour.
I follow the first step:
First you need to configure your databases like below:
return [
'components' => [
'db1' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=database1',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
'db2' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=database2',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
],
]; ?>
But in my configuration in my db it gives me this error:
The configuration for the "db" component must contain a "class"
element.

This is because db component is main and required and you simply omitted its declaration.
Rename db1 and db2 for example to db and db1 accordingly:
return [
'components' => [
// Main connection
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=database1',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
// Another connection
'db1' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=database2',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
],
]; ?>
Update:
Note that for the basic application db is configured in separate file config/db.php and then required in main config config/web.php like so:
'db' => require(__DIR__ . '/db.php'),
So you can configure main connection in db.php and add additional below as db1.

Simply you just have to create separate file for each database .
config/db ->
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=database1',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
To access another db create file in config named db1.php add add another db configuration.
config/db1 ->
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=database2',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
In config->web.php add
'db' => require(__DIR__ . '/db.php'),
'db1' => require(__DIR__ . '/db1.php'),
To access the Model:
public static function getDb() {
return Yii::$app->db1;
}

Related

encoding problems postgresql

I am trying to do a search for Cyrillic names(rus name), but the function returns an empty list.
doctrine config
'doctrine' => [
'connection' => [
'dbname' => 'app',
'user' => 'app',
'password' => 'secret',
'host' => 'db',
'driver' => 'pdo_pgsql',
]
]
Search function:
$qb->andWhere('LOWER(user.name) LIKE :searchTerm')
->setParameter('searchTerm', strtolower('%' . $query['name'] . '%'));
What i tried:
'charset' => 'SQL_ASCII' and 'charset' => 'utf8'

Yii2 advanced with MongoDB

I'm new to Yii2.
I want to use MongoDB with Yii2-advanced. For that, I have found this library:
https://github.com/yiisoft/yii2-mongodb
The issue is it doesn't explain much about the installation and configurations.
As the read me says I have added following into the common\config\main-local.php file:
'mongodb' => [
'class' => '\yii\mongodb\Connection',
'dsn' => 'mongodb://developer:password#localhost:27017/mydatabase',
],
But what should I do with the default db key which is there for of the MySQL connection:
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=yii2_advanced_mongo',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
],
If I comment out that db key and try to login, then it gives me following error:
Invalid Configuration – yii\base\InvalidConfigException
Unknown component ID: db
I think you should add the entry properly, if you want use the entry db for accessing your db datas you should comment the db entry related to mysql and assign db to your mongodb entry
'db' => [
'class' => '\yii\mongodb\Connection',
'dsn' => 'mongodb://your_user_name:your_password#localhost:27017/your_database',
],
//'db' => [
// 'class' => 'yii\db\Connection',
// 'dsn' => 'mysql:host=localhost;dbname=yii2_advanced_mongo',
// 'username' => 'root',
// 'password' => 'root',
// 'charset' => 'utf8',
//],

yii,migrate project to another server

I get a project from some provider and I try to migrate the site to another server and all lock fine only I can't make the connection to db.
The original code is this:
<?php
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=******',
'username' => 'root',
'password' => '********',
'charset' => 'utf8',
];
and the code then I modify is this
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost:3306;dbname=*******',
'username' => 'evoprom_user',
'password' => '********',
'charset' => 'utf8',
];
Do you have some idea?

How to setup module's own database

I want to setup module's database independent from base project and configure it from config/web.php like the following:
'modules' => [
'user' => [
///...
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'pgsql:host=127.0.0.1;port=5432;dbname=klabs',
'username' => 'postgres',
'password' => '',
'charset' => 'utf8',
]
]
]
Is it available to make so? And how to do it, if yes?
First, you need config other database components like that:
...
'components' => [
'db1' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=db1name',
'username' => 'db1username',
'password' => 'db1password',
],
'db2' => [
'class' => 'yii\db\Connection',
'dsn' => 'pgsql:host=localhost;port:5432dbname=db2name',
'username' => 'db2username',
'password' => 'db2password',
],
],
...
And choose db component you defined before in your module config:
'modules' => [
'user' => [
'class' => 'other\to\UserModule',
'layout' => 'main',
'db' => 'db2', // or db1
...
],
],
Hope it helpful.
Goodluck and have fun!

Multi-tenant SaaS built in Yii2

I am working in a multi-tenant software (SaaS) built with yii2 on the Advanced Template, but I am not having the desired result about the tenants database connection.
I am trying to set the Database Connection as next in my config file for the frontend:
$defaultAdminDB = [
'class' => 'yii\db\Connection',
'dsn' => 'pgsql:host=localhost;dbname=untitled',
'username' => 'postgres',
'password' => 'myPass',
'charset' => 'utf8',
];
$config = [
'components' => [
'db' => function(){
if (Yii::$app->session->get('login', false)){
return [
'class' => 'yii\db\Connection',
'dsn' => Yii::$app->session->get('client_connection.dns'),
'username' => Yii::$app->session->get('client_connection.username'),
'password' => Yii::$app->session->get('client_connection.password'),
'charset' => 'utf8',
];
}
return [
'class' => 'yii\db\Connection',
'dsn' => 'pgsql:host=localhost;dbname=untitled',
'username' => 'postgres',
'password' => 'myPass',
'charset' => 'utf8',
];
},
'dbAdmin' => $defaultAdminDB
]
];
Then I have a two steps log in, where the first asks for the login (tenant id) and the next provides the user and password. On the first controller I do the next with:
$account = \frontend\models\AdminAccounts::findOne(['login'=>$this->login]);
if (!$account){
$this->addError('login', Yii::t('app', 'Account data not found.'));
return false;
}
$dns = sprintf('pgsql:host=%s;dbname=%s', $account->getAttribute('db_host'), $account->getAttribute('db'));
Yii::$app->session->set('login', $this->login);
Yii::$app->session->set('client_connection.dns', $dns);
Yii::$app->session->set('client_connection.username', $account->getAttribute('db_user'));
Yii::$app->session->set('client_connection.password', $account->getAttribute('db_pass'));
I get successfully the account (tenant) data and store it in session, but I guess my error is on instantiating the yii\db\Connection on the model's getDb() method.
Hope can help me.
Regards!
If you are changing the db connection info of an existing db connection then you should close the existing connection (Yii::$app->db->close()), change the connection info, and then open the new connection (Yii::$app->db->open()).
You can try
Yii::$app->db = new \yii\db\Connection([
'dsn' => Yii::$app->session->get('client_connection.dsn'),
'username' => Yii::$app->session->get('client_connection.username'),
'password' => Yii::$app->session->get('client_connection.password'),
]);

Categories