How i can connect to Oracle database with Yii2?

半腔热情 提交于 2019-12-11 16:49:30

问题


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:


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



来源:https://stackoverflow.com/questions/50279984/how-i-can-connect-to-oracle-database-with-yii2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!