我們先來看一下setInc的官方示例:

需要一個字段和一個自增的值(默認為1)
我們通過下面這個例子來一步步分析他的底層是怎么實現(xiàn)的:
?phpnamespace Home/Controller;use Think/Controller;html' target='_blank'>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下
/** * 字段值增長 * @access public * @param string $field 字段名 * @param integer $step 增長值 * @return boolean public function setInc($field,$step=1) { return $this- setField($field,array( exp ,$field. + .$step)); }
可以看到這里用到了setField這個方法,然后用exp自定義表達式設(shè)置 $field = $field + $step 到這里,我們稍微了解了一點原理。
可是問題又來了setField又是怎么實現(xiàn)的呢?在同個文件下,找到setField方法:
/** * 設(shè)置記錄的某個字段值 * 支持使用數(shù)據(jù)庫字段和方法 * @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; 其實就是 $data[ test_number ] = array( exp , test_number+2 )
接著來看最常用的save方法:
/** * 保存數(shù)據(jù) * @access public * @param mixed $data 數(shù)據(jù) * @param array $options 表達式 * @return boolean public function save($data= ,$options=array()) { if(empty($data)) { // 沒有傳遞數(shù)據(jù),獲取當前數(shù)據(jù)對象的值 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); // 分析表達式 $options = $this- _parseOptions($options); $pk = $this- getPk(); if(!isset($options[ where ]) ) { // 如果存在主鍵數(shù)據(jù) 則自動作為更新條件 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的字符串數(shù)組,后者調(diào)用了proj/tptest/ThinkPHP/Library/Think/Db.class.php下的update方法:
/** * 更新記錄 * @access public * @param mixed $data 數(shù)據(jù) * @param array $options 表達式 * @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())); }最后其實就是用到了proj/ThinkPHP/Library/Think/Db/Driver/Mysql.class.php這個驅(qū)動類的execute方法。
/** * 執(zhí)行語句 * @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í)行時間 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語句。
到此為止,setInc的源碼已經(jīng)大致過了一遍了。想必大家對setInc如何執(zhí)行也更了解了一點。
以上這篇thinkphp3.2.0 setInc方法 源碼全面解析就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持php 。
相關(guān)推薦:
PHP 使用二進制保存用戶狀態(tài)的實例php技巧
PHP+Redis 消息隊列 實現(xiàn)高并發(fā)下注冊人數(shù)統(tǒng)計的實例php實例
laravel ORM 只開啟created_at的幾種方法總結(jié)php實例
以上就是thinkphp3.2.0 setInc方法 源碼全面解析php實例的詳細內(nèi)容,PHP教程
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。
新聞熱點
疑難解答
圖片精選