How i can connect to Oracle database with Yii2?

2019-08-22 11:40发布

We have a school work, and we make a website with yii2 framework.

What is the problem? We cant use migration, what is the best way; we must to use an oracle database. We made it, tables, etc, but we dont know, how to connect this oracle ,,server,, with yii2.

1条回答
疯言疯语
2楼-- · 2019-08-22 12:30

You should go through the docs for Yii2.

You have to add the configuration in the common/config/main.php or config/db.php, depending upon the version of Yii2 (Advanced or Basic), and under the components you have to add the database component settings which are same for all databases, the only thing that differs is the dsn string below are all supported strings, that might help you or others landing on this question.

For Advanced App

Add the following under components

'db' => [
           'class' => 'yii\db\Connection',
           //'dsn' => 'mysql:host=localhost;dbname=mydatabase', // 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' => 'DB_USERNAME',
          'password' => 'DB_PASSWORD',
          'charset' => 'utf8',
]

For Basic App

Just note that if you are using basic-app for Yii2 you should copy below inside the db.php

<?php 
return [
    'class' => 'yii\db\Connection' ,
    'dsn' => 'oci:dbname=//localhost:1521/mydatabase', // Oracle
    'username' => 'DB_USERNAME',
    'password' => 'DB_PASSWORD',
    'charset' => 'utf8' ,
];

and make sure you have

    'db' => $db,

included in the config/web.php under the components

查看更多
登录 后发表回答