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

  • SEARCH
  • Home
  • 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 8230153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:02:36+00:00 2026-06-07T17:02:36+00:00

I have two database connections, one that is used for most of my application

  • 0

I have two database connections, one that is used for most of my application data, and one that is only used for reads.

Although I can setup my database user account to only allow reads, there are other people administering this system, and I want some redundancy at the application level to absolutely prevent unintended writes using the Yii’s standard ActiveRecord classes.

Found this bit of information on the forums, but was wondering if someone could confirm that this is a good approach and/or suggest another one.

public function onBeforeSave($event)
{
   $this->db = Yii::app()->masterDb;
}

public function onAfterSave($event)
{
   $this->db = Yii::app()->db;
}

http://www.yiiframework.com/forum/index.php/topic/5712-active-record-save-to-different-server-load-balancefail-over-setup/

  • 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-07T17:02:37+00:00Added an answer on June 7, 2026 at 5:02 pm

    Per that link you provided to the Yii forums, there’s an extension that handles this for you:
    http://www.yiiframework.com/extension/dbreadwritesplitting

    I’d probably look into that first, if you’ve got a lot of AR models. You could go the Behavior route (as suggested in that forum post) as another option.

    But whatever you do, you are going to want to be overriding beforeSave / afterSave instead of onBeforeSave / onAfterSave. Those methods are for triggering events, not just running your own special code. And, per another one of the forum posts, you’ll need to set your AR db variable using a static call. So Sergey’s code should actually be:

    class MyActiveRecord extends CActiveRecord
    {
        ...
        public function beforeSave()
        {
           // set write DB
           self::$db = Yii::app()->masterDb;
    
           return parent::beforeSave();
        }
    
        public function afterSave()
        {
           // set read db 
           self::$db = Yii::app()->db;
    
           return parent::beforeSave();
        }
        ...
    }
    
    
    class User extends MyActiveRecord {}
    class Post extends MyActiveRecord {}
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two Database, DB1 and DB2. How can I transfer data of DB1
I have two database one with relationship(no data) and another with out Relationship (with
I have a SQLite database that is used by two processes. I am wondering,
I have two classes that represent two different database entities. Their relationship is 1:m
I have a database two tables and a linking table that I need a
I have two tables in my database schema that represent an entity having a
I have a form in my vb.net application used to take the data about
I have compared two queries which fetch some fairly large data from a database
We have a Java desktop application that accesses a Derby database located on a
I have an iPhone (iOS) app that keeps data in a local SQLite database

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.