如何使用Yii2连接到Oracle数据库?

如何使用Yii2连接到Oracle数据库?

问题描述:

我们有一份学校作业,并且使用yii2框架制作了一个网站.

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

出什么问题了?我们不能使用迁移,最好的方法是什么?我们必须使用一个oracle数据库.我们做了表等,但是我们不知道如何用yii2连接这个oracle服务器.

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.

您应仔细阅读 docs for Yii2.

You should go through the docs for Yii2.

您必须根据Yii2的版本(高级或基本)在common/config/main.phpconfig/db.php中添加配置,并且必须在组件下添加与所有数据库相同的数据库组件设置,唯一不同的是下面的dsn字符串是所有受支持的字符串,这可能有助于您或其他了解此问题的人.

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.

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

请注意,如果您要对Yii2使用basic-app,则应在db.php

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' ,
];

并确保您拥有

    'db' => $db,

包含在components