0

I'm not 100% sure that a module is the right way to go here. So thought i'd ask. My use case is that I have a fairly large application that is powered from an oracle DB.

We also have another DB which isn't oracle and would be involved in a different type of work and a different user group, so I thought I should use a module - Is that right? If not what's the best way to achieve do this kind of set up?

If so, is there a way to configure this second DB within my Module or should it be done within the main app/config/dp.php file?

9
  • You are using Basic or Advanced Application? Commented Sep 7, 2016 at 14:06
  • @NanaPartykar Basic Commented Sep 7, 2016 at 14:07
  • Then, create one more db1.php and include in config/web.php like as it is declared in web.php i.e. 'db1' => require(__DIR__ . '/db1.php'), Commented Sep 7, 2016 at 14:08
  • But I'm ok to keep a module? Commented Sep 7, 2016 at 14:11
  • See. I don't know which module you are talking of. Commented Sep 7, 2016 at 14:23

3 Answers 3

1

Since, you are using Yii2-Basic Application. So, Directory structure is like.

Root Folder
    -> assets
    -> commands
    -> components
    -> config
        -> console.php
        -> db.php
        -> params.php
        -> web.php
    -> controllers
    -> mail
    .
    .
    .

Your database connection details are present in db.php. Now, you want another Database to play important rle in your application. No worries. Create one more database connection details in,say, db2.php inside config folder.

db2.php

<?php

return [
  'class' => 'yii\db\Connection',
  'dsn' => 'mysql:host=localhost;dbname=another_database_name',
  'username' => 'username',
  'password' => 'password',
  'charset' => 'utf8',
];

So, now directory structure will be :

Root Folder
    -> assets
    -> commands
    -> components
    -> config
        -> console.php
        -> db.php
        -> db2.php
        -> params.php
        -> web.php
    -> controllers
    -> mail
    .
    .
    .

Now, next step to include db2.php in the application.

Open web.php. Add one more line for db2.php

<?php

$params = require(__DIR__ . '/params.php');

$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'components' => [
        .
        .
        .
        'db' => require(__DIR__ . '/db.php'),
        'db2' => require(__DIR__ . '/db2.php'),
    ],
];

So, Another database connection is also set successfully. Now, next step is to use query related to another database connection.

$row = Yii::$app->db2->createCommand("SELECT * FROM `table_name`")->queryOne();
Sign up to request clarification or add additional context in comments.

Comments

1

In config/web.php add your second database configuration, create a file called db2.php and call it

'db' => require(__DIR__ . '/db.php'),    
'db2' => require(__DIR__ . '/db2.php'),

in the model,

 class MyModel extends \yii\db\ActiveRecord {

   // add the function below:
   public static function getDb() {
       return Yii::$app->get('db2'); // second database
   }

Comments

1

A simple way is define two different component one of each db eg db1 for oracle and db2 for mysql in you config/web.php

'components' => [
    ......
    'db1' => [
        'class' => 'yii\db\Connection',
        'dsn' => 'oci:dbname=//localhost:1521/mydatabase',
        'username' => 'scott',
        'password' => 'tiger',
        'charset' => 'utf8',
    ],
    'db2' => [
        'class' => 'yii\db\Connection',
        'dsn' => 'mysql:host=localhost;dbname=mydbname',
        'username' => 'root',
        'password' => '',
        'charset' => 'utf8',
    ],

and the refer to Yii::$app->db1 or Yii::$app->db2 eg:

$count1 = Yii::$app->db1->createCommand('SELECT COUNT(*) FROM post')
         ->queryScalar();

or

$count2 = Yii::$app->db2->createCommand('SELECT COUNT(*) FROM post')
         ->queryScalar();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.