Country.php
<?php
namespace app\models;
use yii\db\ActiveRecord;
class Country extends ActiveRecord
{
}
现在我使用上面的类使用默认的db配置
来查询Country表use app\models\Country;
// get all rows from the country table and order them by "name"
$countries = Country::find()->orderBy('name')->all(); // this line uses default db configuration
保护/配置/ main.php
return array(
'components' => array(
'db' => require(dirname(__FILE__).'/../../db_config.php'),
'dba' => require(dirname(__FILE__).'/../../dba_config.php'),
'dbmobisite' => require(dirname(__FILE__).'/../../dbmobisite_config.php'),
'dbamobisite' => require(dirname(__FILE__).'/../../dbamobisite_config.php'),
)
)
现在我改变了:
$db = Yii::app()->getComponent('dbmobisite'); //wanted to use different db
$db->setActive(true); //changing it to active
$countries = Country::find()->orderBy('name')->all(); //but this still queries using "db" configuration not "dbmobisite" configuration.
我想&#34; dbmobisite&#34;仅适用于单个查询。可能吗?如果可能请帮助我。
提前致谢
答案 0 :(得分:0)
我认为最简单的解决方案是创建一个由此扩展的新类并覆盖getDb()方法,例如:
<?php
namespace app\models;
use yii\db\ActiveRecord;
class CountryOtherDb extends Country
{
public function getDb()
{
return \Yii::$app->otherDb
}
}