Hello, I have problem.
I have objects` db. here is db_name db_username db_password.
I need that when I select object it would create a connection with its own database and would further work with it…
Hello, I have problem.
I have objects` db. here is db_name db_username db_password.
I need that when I select object it would create a connection with its own database and would further work with it…
I share my code for this problem.
Object class
/**
* @params object from database with db connection inforamtion
* @return db connection parameters
*/
public function setDb(){
$definition = [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=10.100.1.40;dbname=db_'.$this->company_code,
'username' => 'user_'.$this->company_code,
'password' => $this->db_password,
'charset' => 'utf8',
];
Yii::$app->set('dbd',$definition);
}
Extended ActiveRecord class
namespace app\models;
use Yii;
class ModelBaseDifferentDb extends ModelBases
{
/**
* @return db connection parameters
*/
public static function getDb()
{
return Yii::$app->get('dbd');
}
}
when I sellected object
// in first $model object I have only id from sellect form.
// load all connection data from database
$model= $model->findOne($model->id);
$model->setDb();
In another class:
class Classname extends ModelBaseDifferentDb
{
/**
* class function
*/
}
Sorry for my english hopes this helps
P.S. I waiting comments. Its good solution or no?