Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii restrict database connection to read-only

Tags:

php

yii

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/

like image 563
John Zumbrum Avatar asked Jul 09 '12 01:07

John Zumbrum


3 Answers

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 {}
...
like image 80
acorncom Avatar answered Nov 17 '22 22:11

acorncom


Given a scenario where your slave can't update with the master, you might run into problems. Because after updating data you'll maybe read from an old version.

While the given approaches in the forum are very clean and written by authors which are mostly Yii wizards. I also have an alternative. You may override the getDbConnection() method in AR like

public function getDbConnection(){
  if (Yii::app()->user->hasEditedData()) { # you've got to write something like this(!)
     return Yii::app()->masterDb;
  } else {
     return Yii::app()->db;
  }
}

But you still have to be careful when switching database connections.

like image 25
schmunk Avatar answered Nov 17 '22 23:11

schmunk


class MyActiveRecord extends CActiveRecord
{
...
public function onBeforeSave($event)
{
   // set write DB
   $this->db = Yii::app()->masterDb;
}

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


class User extends MyActiveRecord {}
class Post extends MyActiveRecord {}
...

You have to try that way. But in my opinion, it's not good enough. I think there will be some bugs or defects.

like image 2
Sergey Avatar answered Nov 17 '22 23:11

Sergey