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

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.

解决方案

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值