我們先來(lái)看一下setInc的官方示例:
需要一個(gè)字段和一個(gè)自增的值(默認(rèn)為1)
我們通過(guò)下面這個(gè)例子來(lái)一步步分析他的底層是怎么實(shí)現(xiàn)的:
?php
namespace Home\Controller;
use Think\Controller;
class TestController extends Controller {
public function test() {
$tb_test = M('test');
$tb_test->where(['id'=>1])->setInc('test_number',2); //每次添加2
dump($tb_test->getLastSql());
//string(67) "UPDATE `tb_test` SET `test_number`=test_number+2 WHERE ( `id` = 1 )"
}
}
第一步肯定是要找到setInc方法的源碼:
這里我用到了phpstrom全局搜索的方法,找到了setInc是在proj\ThinkPHP\Library\Think\Model.class.php下
/**
* 字段值增長(zhǎng)
* @access public
* @param string $field 字段名
* @param integer $step 增長(zhǎng)值
* @return boolean
*/
public function setInc($field,$step=1) {
return $this->setField($field,array('exp',$field.'+'.$step));
}
可以看到這里用到了setField這個(gè)方法,然后用exp自定義表達(dá)式設(shè)置 $field = $field + $step 到這里,我們稍微了解了一點(diǎn)原理。
可是問題又來(lái)了setField又是怎么實(shí)現(xiàn)的呢?在同個(gè)文件下,找到setField方法:
/**
* 設(shè)置記錄的某個(gè)字段值
* 支持使用數(shù)據(jù)庫(kù)字段和方法
* @access public
* @param string|array $field 字段名
* @param string $value 字段值
* @return boolean
*/
public function setField($field,$value='') {
if(is_array($field)) {
$data = $field;
}else{
$data[$field] = $value;
}
return $this->save($data);
}
這里我們看到了常用到的save方法,這里的 $data[$field] = $value; 其實(shí)就是 $data['test_number'] = array("exp","test_number+2")
接著來(lái)看最常用的save方法:
/**
* 保存數(shù)據(jù)
* @access public
* @param mixed $data 數(shù)據(jù)
* @param array $options 表達(dá)式
* @return boolean
*/
public function save($data='',$options=array()) {
if(empty($data)) {
// 沒有傳遞數(shù)據(jù),獲取當(dāng)前數(shù)據(jù)對(duì)象的值
if(!empty($this->data)) {
$data = $this->data;
// 重置數(shù)據(jù)
$this->data = array();
}else{
$this->error = L('_DATA_TYPE_INVALID_');
return false;
}
}
// 數(shù)據(jù)處理
$data = $this->_facade($data);
// 分析表達(dá)式
$options = $this->_parseOptions($options);
$pk = $this->getPk();
if(!isset($options['where']) ) {
// 如果存在主鍵數(shù)據(jù) 則自動(dòng)作為更新條件
if(isset($data[$pk])) {
$where[$pk] = $data[$pk];
$options['where'] = $where;
unset($data[$pk]);
}else{
// 如果沒有任何更新條件則不執(zhí)行
$this->error = L('_OPERATION_WRONG_');
return false;
}
}
if(is_array($options['where']) isset($options['where'][$pk])){
$pkValue = $options['where'][$pk];
}
if(false === $this->_before_update($data,$options)) {
return false;
}
$result = $this->db->update($data,$options);
if(false !== $result) {
if(isset($pkValue)) $data[$pk] = $pkValue;
$this->_after_update($data,$options);
}
return $result;
}
最主要是的$options = $this->_parseOptions($options);和$result = $this->db->update($data,$options); 前者把參數(shù)轉(zhuǎn)換成用于拼接sql的字符串?dāng)?shù)組,后者調(diào)用了proj\tptest\ThinkPHP\Library\Think\Db.class.php下的update方法:
/**
* 更新記錄
* @access public
* @param mixed $data 數(shù)據(jù)
* @param array $options 表達(dá)式
* @return false | integer
*/
public function update($data,$options) {
$this->model = $options['model'];
$sql = 'UPDATE '
.$this->parseTable($options['table'])
.$this->parseSet($data)
.$this->parseWhere(!empty($options['where'])?$options['where']:'')
.$this->parseOrder(!empty($options['order'])?$options['order']:'')
.$this->parseLimit(!empty($options['limit'])?$options['limit']:'')
.$this->parseLock(isset($options['lock'])?$options['lock']:false)
.$this->parseComment(!empty($options['comment'])?$options['comment']:'');
return $this->execute($sql,$this->parseBind(!empty($options['bind'])?$options['bind']:array()));
}
最后其實(shí)就是用到了proj\ThinkPHP\Library\Think\Db\Driver\Mysql.class.php這個(gè)驅(qū)動(dòng)類的execute方法。
/**
* 執(zhí)行語(yǔ)句
* @access public
* @param string $str sql指令
* @return integer|false
*/
public function execute($str) {
$this->initConnect(true);
if ( !$this->_linkID ) return false;
$this->queryStr = $str;
//釋放前次的查詢結(jié)果
if ( $this->queryID ) { $this->free(); }
N('db_write',1);
// 記錄開始執(zhí)行時(shí)間
G('queryStartTime');
$result = mysql_query($str, $this->_linkID) ;
$this->debug();
if ( false === $result) {
$this->error();
return false;
} else {
$this->numRows = mysql_affected_rows($this->_linkID);
$this->lastInsID = mysql_insert_id($this->_linkID);
return $this->numRows;
}
}
最后用最底層的mysql_query執(zhí)行SQL語(yǔ)句。
到此為止,setInc的源碼已經(jīng)大致過(guò)了一遍了。想必大家對(duì)setInc如何執(zhí)行也更了解了一點(diǎn)。
以上這篇thinkphp3.2.0 setInc方法 源碼全面解析就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。