• <legend id='uEhoA'><style id='uEhoA'><dir id='uEhoA'><q id='uEhoA'></q></dir></style></legend>

    <small id='uEhoA'></small><noframes id='uEhoA'>

      <tfoot id='uEhoA'></tfoot>

      1. <i id='uEhoA'><tr id='uEhoA'><dt id='uEhoA'><q id='uEhoA'><span id='uEhoA'><b id='uEhoA'><form id='uEhoA'><ins id='uEhoA'></ins><ul id='uEhoA'></ul><sub id='uEhoA'></sub></form><legend id='uEhoA'></legend><bdo id='uEhoA'><pre id='uEhoA'><center id='uEhoA'></center></pre></bdo></b><th id='uEhoA'></th></span></q></dt></tr></i><div id='uEhoA'><tfoot id='uEhoA'></tfoot><dl id='uEhoA'><fieldset id='uEhoA'></fieldset></dl></div>
        • <bdo id='uEhoA'></bdo><ul id='uEhoA'></ul>

        Yii2 在运行时设置数据库连接

        时间:2023-10-16
        • <legend id='KLTFC'><style id='KLTFC'><dir id='KLTFC'><q id='KLTFC'></q></dir></style></legend>

                <tbody id='KLTFC'></tbody>
              <i id='KLTFC'><tr id='KLTFC'><dt id='KLTFC'><q id='KLTFC'><span id='KLTFC'><b id='KLTFC'><form id='KLTFC'><ins id='KLTFC'></ins><ul id='KLTFC'></ul><sub id='KLTFC'></sub></form><legend id='KLTFC'></legend><bdo id='KLTFC'><pre id='KLTFC'><center id='KLTFC'></center></pre></bdo></b><th id='KLTFC'></th></span></q></dt></tr></i><div id='KLTFC'><tfoot id='KLTFC'></tfoot><dl id='KLTFC'><fieldset id='KLTFC'></fieldset></dl></div>
                <bdo id='KLTFC'></bdo><ul id='KLTFC'></ul>

                <small id='KLTFC'></small><noframes id='KLTFC'>

              • <tfoot id='KLTFC'></tfoot>
                • 本文介绍了Yii2 在运行时设置数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在我的 Yii2(基本应用程序)web.php 中,我将 NULL db 连接配置为第二个数据库连接.

                  In my Yii2 (basic application) web.php I configure a NULL db connection as 2nd database connection.

                  这需要填充来自主数据库连接记录的有效参数:

                  This needs to be filled with valid parameters which are coming from a record on the main db connection:

                  'db' => require(__DIR__ . '/db.php'),
                  'db2' => [
                      'class' => 'yiidbConnection',
                      'dsn' => NULL,
                      'username' => NULL,
                      'password' => NULL,
                      'charset' => 'utf8',
                  ],
                  

                  初始化 app() 后,我需要使用从另一个数据库中检索到的值填写 NULL 参数,以便在模型中进一步使用它.

                  After initializing the app() i need to fill out the NULL parameters with values that i retrieve from another database to further use it in models.

                  我如何在 Yii2 中实现这一点?

                  How can i achieve this in Yii2?

                  推荐答案

                  没问题,支持

                  Yii::$app->db2->close(); // make sure it clean
                  Yii::$app->db2->dsn= 'yourdsn';
                  Yii::$app->db2->username = 'username';
                  Yii::$app->db2->password = 'password';
                  

                  完成,现在可以使用了

                  Yii::$app->db2->...
                  

                  另一种方式:

                  $connection = new yiidbConnection([
                      'dsn' => $dsn,
                      'username' => $username,
                      'password' => $password,
                  ]);
                  $connection->open();
                  $command = $connection->createCommand('SELECT * FROM post')->....;
                  

                  参考:http://www.yiiframework.com/doc-2.0/yii-db-connection.html

                  这篇关于Yii2 在运行时设置数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:codecept:未找到命令 下一篇:使用 Yii2 的 GridView 中的默认过滤器

                  相关文章

                  <tfoot id='lIrTk'></tfoot>

                    <small id='lIrTk'></small><noframes id='lIrTk'>

                  1. <legend id='lIrTk'><style id='lIrTk'><dir id='lIrTk'><q id='lIrTk'></q></dir></style></legend>
                    <i id='lIrTk'><tr id='lIrTk'><dt id='lIrTk'><q id='lIrTk'><span id='lIrTk'><b id='lIrTk'><form id='lIrTk'><ins id='lIrTk'></ins><ul id='lIrTk'></ul><sub id='lIrTk'></sub></form><legend id='lIrTk'></legend><bdo id='lIrTk'><pre id='lIrTk'><center id='lIrTk'></center></pre></bdo></b><th id='lIrTk'></th></span></q></dt></tr></i><div id='lIrTk'><tfoot id='lIrTk'></tfoot><dl id='lIrTk'><fieldset id='lIrTk'></fieldset></dl></div>
                    • <bdo id='lIrTk'></bdo><ul id='lIrTk'></ul>