Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8646833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:54:55+00:00 2026-06-12T12:54:55+00:00

i read the yii docs and the following code should work; well, it doesnt.

  • 0

i read the yii docs and the following code should work;

well, it doesnt. :))

db is the primary database

db1 and db2 are the secondary databases

what is wrong here ?

the website is online, at www.linkbook.co, and it cant connect to any of the databases

'db' => array(
    'connectionString' => 'mysql:host=localhost;dbname=linkbookco',
    'emulatePrepare' => true,
    'username' => 'user',
    'password' => 'password',
    'charset' => 'utf8',
    'tablePrefix' => '',
),

    'db1' => array(
    'connectionString' => 'mysql:host=localhost;dbname=linkbookco1',
    'username'         => 'user',
    'password' => 'password',
    'charset' => 'utf8',
    'tablePrefix' => '',
    'class'            => 'CDbConnection'          // DO NOT FORGET THIS!
),

    'db2' => array(
    'connectionString' => 'mysql:host=localhost;dbname=linkbookco2',
    'username'         => 'user',
    'password' => 'password',
    'charset' => 'utf8',
    'tablePrefix' => '',
    'class'            => 'CDbConnection'          // DO NOT FORGET THIS!
),
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:54:57+00:00Added an answer on June 12, 2026 at 12:54 pm

    db is the predefined component of Yii, so bedefault CActiveRecord makes connection using db. So for other components you have created using CDbConnection class you have to activate connection for them externally.

    SO You need to overwrite getDbConnection() method of CActiveRecord.

    Extend the CActiveRecord for specific database connection like db1.
    Save this as Db1CActiveRecord.php and place in components directory.

    <?php
    /**
     * 
     * Used for db1 database connection
     *
     */
    class Db1CActiveRecord extends CActiveRecord {
    
        private static $db1 = null;
    
        public function getDbConnection()
        {
            if (self::$db1 !== null)
                return self::$db1;
            else
            {
                self::$db1 = Yii::app()->db1;
                if (self::$db1 instanceof CDbConnection)
                {
                    self::$db1->setActive(true);
                    return self::$db1;
                }
                else
                    throw new CDbException(Yii::t('yii','Active Record requires a "db" CDbConnection application component.'));
            }
        }
    }
    

    Now you need to use Db1CActiveRecord for the model class of database db1.
    Like:

    class Db1Model extends Db1CActiveRecord{
       ......
    }
    

    Implement this way for both db1 & db2 database.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Read that the following code is an example of unsafe construction as it allows
read the following code: // Creiamo la toolbar sotto UIToolbar *toolbar = [[UIToolbar alloc]
After running into the Yii asset engine I've read the official documentation (http://www.yiiframework.com/wiki/148/understanding-assets/). I
read a lot about DataAdapter, DataTable ,.. to reach to this code, in the
Read this question today about safe and unsafe code I then read about it
Read a question on stack overflow sometime back with the following syntax In [1]:
I haven't seen this scenario covered here: Yii Framework: How to work with Flash
Read through this code. I wrote it at my desk in 5 minutes, it's
Read this on the oracle docs java.lang page: Frequently it is necessary to represent
I was searching for a Yii file helper to make read and write operation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.