2013-07-04 1 views
0

여러 데이터베이스를 사용할 때 모든 활성 연결을 얻는 방법은 무엇입니까?모든 활성 DB 연결 가져 오기

foreach(Yii::app()->getComponents() as $component) 
{  
    if ($component instanceof CDbConnection) 
    { 
     die(var_dump($component)); 
    } 
} 

을하지만하지 Yii::app()->getComponents() 결과에 데이터베이스 구성 요소 모양입니다 :

이 나는 ​​노력했다.

내 DB를 설정 :

'db'=>array(
     'connectionString' => 'pgsql:host=127.0.0.1;port=yyyy;dbname=db1', 
     'emulatePrepare' => false, 
     'username' => 'user1', 
     'password' => 'pass1', 
     'schemaCachingDuration' => YII_DEBUG ? 0 : 86400000, // 1000 days 
     'enableParamLogging' => YII_DEBUG, 
     'charset' => 'utf8' 
), 

'db2'=>array(
     'class' => 'CDbConnection', 
     'connectionString' => 'pgsql:host=xxx.xxx.xxx;port=xxxx;dbname=db2', 
     'emulatePrepare' => false, 
     'username' => 'user2', 
     'password' => 'pass2', 
     'schemaCachingDuration' => YII_DEBUG ? 0 : 86400000, // 1000 days 
     'enableParamLogging' => YII_DEBUG, 
     'charset' => 'utf8' 
), 
+0

@Dagon 어디서 표시할까요? –

+0

당신은'Yii :: app() -> db-> getactive()'또는'if (Yii :: app-> db-> active)' – ineersa

+0

으로 확인할 수 있습니다. 그러나 이것은 이상한 getComponents()가 작동해야합니다. – ineersa

답변

0

당신이 CActiveRecord 인스턴스 ($ 모델), 당신이 $model->getDbConnection(); 또는 CActiveRecord::$db가 현재 연결 데이터베이스를 얻을하는 데 사용할 수있는 경우;

0

클래스 참조에서는 읽을 수

공공 배열 getComponents (부울 $ loadedOnly = TRUE)

그래서, 당신은 대신에만 활성의 모든 연결을 얻으려면 :

foreach (Yii::app()->getComponents(false) as $component) 
{ 
    if (is_array($component) && $component['class'] == 'CDbConnection') 
    { 
     print_r($component); 
    } 
} 

모두 표시해야합니다.

관련 문제