Home > Yii > Eine zweite DbConnection

Eine zweite DbConnection

'components' => array(
    'db' => array(
        'connectionString' => 'mysql:host=localhost;dbname=db1',
        'emulatePrepare' => true,
        'username' => 'root',
        'password' => '',
        'charset' => 'utf8',
    ),
    'db2' => array(
        'class' => 'CDbConnection', //wichtig!!
        'connectionString' => 'mysql:host=localhost;dbname=db2',
        'emulatePrepare' => true,
        'username' => 'root',
        'password' => '',
        'charset' => 'utf8',
    ),
),
 
$rows = Yii::app()->db->createCommand('SELECT * FROM tbl_user')->queryAll();
$rows2 = Yii::app()->db2->createCommand('SELECT * FROM tbl_user')->queryAll();
 
/*
 * soll ActiveRecord eine andere DbConnection benutzen, dann so
 */
public function getDbConnection()
{
    return Yii::app()->getComponent('db2');
}

Verwandte Artikel:

  1. Parameterized Named Scopes
  2. Gegen DB authentifizieren
  3. Wie ich Datenbank-Tabellen und -Felder benenne
  4. yiiframework – Guidelines for good schema design
Author: Categories: Yii Tags:
  1. Bisher keine Kommentare
  1. Bisher keine Trackbacks