環境: ApacheFriends XAMPP Version 7.1.7

  + Apache 2.4.26
  + MariaDB 10.1.25
  + PHP 7.1.7 (VC14 X86 32bit thread safe) + PEAR
  + phpMyAdmin 4.7.0
  + OpenSSL 1.0.2f
  + ADOdb 518a
  + Mercury Mail Transport System v4.63 (not included in the portable version)
  + FileZilla FTP Server 0.9.41 (not included in the portable version)
  + Webalizer 2.23-04 (not included in the portable version)
  + Strawberry Perl 5.16.1.1 Portable
  + Tomcat 7.0.56
  + XAMPP Control Panel Version 3.2.2 by hackattack142

Step01: 先將 php.ini 裡的 extension=php_pdo_oci.dll 注釋移除

; Be sure to appropriately set the extension_dir directive.
;
extension=php_bz2.dll
extension=php_curl.dll
extension=php_fileinfo.dll
;extension=php_ftp.dll
extension=php_gd2.dll
extension=php_gettext.dll
;extension=php_gmp.dll
;extension=php_intl.dll
;extension=php_imap.dll
;extension=php_interbase.dll
;extension=php_ldap.dll
extension=php_mbstring.dll
extension=php_exif.dll      ; Must be after mbstring as it depends on it
extension=php_mysqli.dll
;extension=php_oci8_12c.dll  ; Use with Oracle Database 12c Instant Client
;extension=php_openssl.dll
;extension=php_pdo_firebird.dll
extension=php_pdo_mysql.dll
extension=php_pdo_oci.dll
;extension=php_pdo_odbc.dll
;extension=php_pdo_pgsql.dll
extension=php_pdo_sqlite.dll
;extension=php_pgsql.dll
;extension=php_shmop.dll

Step02: 在 common\config 裡增加兩個檔 dbMaria.php & dbOracle.php

dbMaria.php

<?php
return [
        'class' => 'yii\db\Connection',
        'dsn' => 'mysql:host=localhost;dbname=mysite', // MySQL, MariaDB
        //'dsn' => 'sqlite:/path/to/database/file', // SQLite
        //'dsn' => 'pgsql:host=localhost;port=5432;dbname=mydatabase', // PostgreSQL
        //'dsn' => 'cubrid:dbname=demodb;host=localhost;port=33000', // CUBRID
        //'dsn' => 'sqlsrv:Server=localhost;Database=mydatabase', // MS SQL Server, sqlsrv driver
        //'dsn' => 'dblib:host=localhost;dbname=mydatabase', // MS SQL Server, dblib driver
        //'dsn' => 'mssql:host=localhost;dbname=mydatabase', // MS SQL Server, mssql driver
        //'dsn' => 'oci:dbname=//localhost:1521/mydatabase', // Oracle
        'username' => 'root',
        'password' => 'password',
        'charset' => 'utf8',
];
?>   

dbOracle.php

<?php
return [
        'class' => 'yii\db\Connection',
        //'dsn' => 'mysql:host=localhost;dbname=mysite', // MySQL, MariaDB
        //'dsn' => 'sqlite:/path/to/database/file', // SQLite
        //'dsn' => 'pgsql:host=localhost;port=5432;dbname=mydatabase', // PostgreSQL
        //'dsn' => 'cubrid:dbname=demodb;host=localhost;port=33000', // CUBRID
        //'dsn' => 'sqlsrv:Server=localhost;Database=mydatabase', // MS SQL Server, sqlsrv driver
        //'dsn' => 'dblib:host=localhost;dbname=mydatabase', // MS SQL Server, dblib driver
        //'dsn' => 'mssql:host=localhost;dbname=mydatabase', // MS SQL Server, mssql driver
        'dsn' => 'oci:dbname=//localhost:1521/xe;charset=AL32UTF8', // Oracle  ;charset=AL32UTF8=>解決中文亂碼
        'username' => 'system',
        'password' => 'password',
];
?>   

Step03 : 修改 common\config 裡的 main-local.php

<?php
return [
    'components' => [
        'db'=>require(__DIR__ . '/dbMaria.php'),
        'db2'=>require(__DIR__ . '/dbOracle.php'),

        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'viewPath' => '@common/mail',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            'useFileTransport' => true,
        ],
    ],
    'timeZone' => 'Asia/Taipei',
];

Step04: 測試:

   /**
     * Displays a single Post model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
//         $post = Yii::$app->db->createCommand('select * from post
//                     where id=:id and status=:status')
//                     ->bindValue(':id', $_GET['id'])
//                     ->bindValue(':status', 2)
//                     ->queryOne();

//         $post = Post::find()->where(['id'=>$id])->one();
//         $post = Post::findOne(['id'=>$id]);
//         var_dump($post);
//         $posts = Post::findAll(['status'=>2]);
//         $posts = Post::find()->where(['and','author_id=1', ['status'=>2],['like','title','yii2']])
//                              ->orderBy('id')->all();        
//         var_dump($posts);

        $users = Yii::$app->db2->createCommand('select * from spfs.USER_DETAILS')->queryAll();        
        var_dump($users);
        exit(0);

        
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

 

 

 

arrow
arrow
    文章標籤
    Yii2 Multiple database
    全站熱搜

    MIS 發表在 痞客邦 留言(0) 人氣()