简体   繁体   中英

Switch between readOnly and writeOnly databases using $useDbConfig in CakePHP?

I have a master and slave database. To balance some of the load, I want all of my INSERT/UPDATE queries to go to the Master and all of my SELECT queries to go to the slave (since you cannot write to a slave without messing it up). I have the databases defined in the database.php. This is how I am trying to do the switching:

Inside the AppModel.php beforeFind() , I am setting:

$this->useDBConfig = 'readOnly';

Inside the beforeSave() , I am setting:

$this->useDBConfig = 'writeOnly';

However, after looking deeper I am wondering if the useDBConfig is a one-to-one relationship model-to-database. Meaning, maybe useDBConfig is only for pointing a single model to a single database, because this does not always work like I would expect. Sometimes an INSERT / UPDATE will attempt to run on a readOnly with readOnly permissions and fails.

If this is the case, how to I change my CakePHP application to send the requests to the appropriate databases? Should I be doing this a different way?

基本上,您可以覆盖AppModel中的所有保存方法(save,saveField,saveAssociated,saveAll,UpdateAll,delete,deleteAll等),并将父调用封装在两个setDatasource调用之间。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM