"src=" http://files.jb51.net/file_images/article/201801/20180129103252.jpg ">

to a field and an increment value (default 1)

us through the following example step by step analysis is how to achieve his bottom:

 < PHP? Namespace HomeController use ThinkController; class TestController; extends Controller function (test) {public {$tb_test = M ('test'); $tb_test-> where (['id'=> 1]) -> setInc ('test_number', 2); / / added 2 dump ($tb_test->); getLastSql (//string); (67) UPDATE `tb_test` SET `test_number`= test_number+2 WHERE (`id` = 1)}}" 

here I use the phpstrom global search method, setInc is found in projThinkPHPLibraryThinkModel.class.php / * * * < /p>

 * public * @access field value growth @param string $field @param integer $step field name * * @return Boolean growth value * public function setInc ($field, $step=1) {return $this-> setField ($field, array ('exp', $field.'+'.$step));}

setField can see where the use of this method, and then use the settings here $field = $field + $step exp custom expression, we know a little about a point of principle.

, but how does setField come true again? In the same file, find the setField method:

 / * * * a field to set the record value * support database fields and methods of public * @access * @param string|array $field * @param string $value field name * @return Boolean * / public field value using function setField ($field, $value='') {if (is_array ($field)) {$data}else{= $field; $data[$field] = $value;} return $this-> save}

($data); here we see the save method commonly used, where $data[$field] = $value; $data['test_number'] = array (in fact is "exp", "test_number+2")

 / * * * * * @param public @access to save the data mixed $data @param array $options expression data * * @return Boolean * / public function ($data=''), $options=array (save) {if (empty ($data)) {/ / no data transfer, access to data objects (current value if! Empty ($this-> data) {$data = $this->) data; $this-> data; / / reset data; (array);}else{= $this-> error = L ('_DATA_TYPE_INVALID_'); return false;}} / / data processing $data = $this-> _facade ($data); / / analysis expression $options = $this-> _parseOptions ($options); $pk = $this-> (getPk); if (! Isset ($options['where'])) {/ / if there are primary data Automatically update as if (isset ($data[$pk])) {$where[$pk] = $data[$pk]; $options['where'] = $where; unset ($data[$pk]}else{); / / if no renewal conditions is not $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}); }

is the main $options = $this-> _parseOptions ($options); and $result = $this-> db-> update ($data, $options); the former is converted to a string array parameter for splicing of SQL, which calls the update method of projtptestThinkPHPLibraryThink Db.class.php:

 / * * * * * @param public @access update mixed $data @param array $options expression data * * @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 (}

))); the last is used in the projThinkPHPLibraryThinkDbDriver Mysql.class.php the driver class execute method.

 / * * * * * @param public @access statement string $str SQL * @return integer|false * / public function instruction execute ($str) {$this-> initConnect (true); if ($this-> return false! _linkID); $this-> queryStr = $str; / / release of the previous query results of if ($this-> queryID) {$this-> (free);} N ('db_write', 1); / / recording start execution time G ('queryStartTime'); $result = mysql_query ($str, $this-> _linkID; $this-> (debug)); if (false = $result) {$this-> (return); error; false;} else {numRows = mysql_affected_rows ($this-> $this-> _linkID; $this-> lastInsID) = mysql_insert_id ($this-> _linkID); return $this-> nu MRows;}}

finally executes the SQL statement with the lowest mysql_query. So far,

, setInc's source code has been roughly over. It's necessary for you to know a little more about how setInc is going to do it.

above, this thinkphp3.2.0 setInc method source code comprehensive analysis is Xiaobian share to you all content, hope to give you a reference, and hope that you support the script home.

This concludes the body part

This paper fixed link:http://www.script-home.com/an-overall-analysis-of-the-source-code-of-the-thinkphp3-2-0-setinc-method.html | Script Home | +Copy Link

Article reprint please specify:An overall analysis of the source code of the thinkphp3.2.0 setInc method | Script Home

You may also be interested in these articles!