Answer the question
In order to leave comments, you need to log in
How to change the $config array?
Good afternoon,
I need to change the config with different button presses.
For example, there is
<?php
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=db',
'username' => 'root',
'password' => '12345',
// 'dsn' => 'mysql:host=serser;dbname=db',
// 'username' => 'root',
// 'password' => '54321',
];
Answer the question
In order to leave comments, you need to log in
Let's say there are 4 files main.php, middle.php, main-local-test.php, main-local-prod.php. In main.php grab the contents of middle.php. middle.php is generated by you when you click on the button, which is loaded either from main-local-test.php or from main-local-prod.php.
PS Do not do this, in the sense of buttons, well, don't, there are better solutions and honestly)
If you need access to several databases, then create 2 connection configs:
db.php
<?php
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=db',
'username' => 'root',
'password' => '12345',
];
<?php
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=serser;dbname=db',
'username' => 'root',
'password' => '54321',
];
'db' => require(__DIR__ . '/db.php'),
'db2' => require(__DIR__ . '/db2.php'),
$posts = Yii::$app-> db ->createCommand('SELECT * FROM post')->queryAll();, from the second
$posts = Yii::$app-> db2 ->createCommand('SELECT * FROM post')->queryAll();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question