Thinkphp是一款不错的国产框架,利用范围广,运用也比较多。随着网站访问增大每每须要利用mysql主从同步功能,本身Thinkphp自带了主从读写分离的功能了。
但是我们常常有一个场景便是某些特定的查询须要从主库进行查询,那该当怎么做呢,下面就分享Thinkphp3.2.3设置MySql主从读写分离后,如何方便的调用主库查询,让大略调用主库数据查询更大略。
基于版本:Thinkphp3.2.x,thinkphp3.x版本整体差不多。
(图片来自网络侵删)修正步骤,紧张是3个地方进行大略修正
1、ThinkPHP/Library/Think/Model.class.php 62行:
protected $methods = array('strict','order','alias','having','group','lock','distinct','auto','filter','validate','result','token','index','force','master'); //1、添加,’master’
2、ThinkPHP/Library/Think/Db/Driver.class.php 月935行:query的时候把master参数传进去
/ 查找记录 @access public @param array $options 表达式 @return mixed/ public function select($options=array()) { $this->model = $options['model']; $sql = $this->buildSelectSql($options); $result = $this->query($sql,$this->parseBind(!empty($options['bind'])?$options['bind']:array()),$options['master']); //2、添加 ,$options['master'] return $result; }
3、ThinkPHP/Library/Think/Db/Driver.class.php 140行
public function query($str,$fetchSql=false,$master=flase) { if(!empty($this->config['deploy']) && ($master || $this->transTimes > 0)){ $this->initConnect(true); }else{ $this->initConnect(false);}
好了,这样就修恰好了。修恰好了之后,在TP系统中调用主库,就比较大略了,如下:
M('User')->master(true)->find(1);
这样就可以直接调用主库的查询了。
有问题欢迎大家留言,以为不错,欢迎收藏关注哦。嘿嘿~~