Thinkphp3.2.0 setInc method full source code parsing,

Source: Internet
Author: User

Thinkphp3.2.0 setInc method full source code parsing,

Let's take a look at the official example of setInc:

A field and an auto-increment value are required (1 by default)

The following example is used to analyze how the underlying layer is implemented step by step:

<? Phpnamespace 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 ); // Add 2 dump ($ tb_test-> getLastSql () each time; // string (67) "UPDATE 'tb _ test' SET 'test _ number' = test_number + 2 WHERE ('id' = 1 )"}}

The first step is to find the source code of the setInc method:

Here I used the phpstrom global search method and found that setInc is under proj \ ThinkPHP \ Library \ Think \ Model. class. php.

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

We can see that the setField method is used here, and then set $ field = $ field + $ step with the exp custom expression here. We have a little understanding of the principle.

But the question is, how is setField implemented? In the same file, find the setField method:

/*** Set a field value for a record * supports using database fields and Methods * @ access public * @ param string | array $ field name * @ param string $ value field value *@ return boolean */public function setField ($ field, $ value = '') {if (is_array ($ field) {$ data = $ field;} else {$ data [$ field] = $ value ;} return $ this-> save ($ data );}

Here we see the commonly used save method. $ data [$ field] = $ value here is actually $ data ['test _ number'] = array ("exp ", "test_number + 2 ")

Next, let's look at the most common save method:

/*** Save data ** @ access public * @ param mixed $ data * @ param array $ options expression * @ return boolean */public function save ($ data = '', $ options = array () {if (empty ($ data) {// No data is transferred, get the value of the current data object if (! Empty ($ this-> data) {$ data = $ this-> data; // reset data $ this-> 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 primary key data exists, it is automatically used as the update condition if (isset ($ data [$ pk]) {$ where [$ pk] = $ data [$ pk]; $ options ['where'] = $ where; unset ($ data [$ pk]);} else {// if no update condition exists, $ this-> error = L ('_ OPERATION_WRONG _'); return false;} is not executed ;}} 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 ;}

Most importantly, $ options = $ this-> _ parseOptions ($ options); and $ result = $ this-> db-> update ($ data, $ options ); the former converts the parameter to a string array used for splicing SQL, and the latter calls proj \ tptest \ ThinkPHP \ Library \ Think \ Db. class. update method in php:

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

Finally, we used the execute method of the Driver class proj \ ThinkPHP \ Library \ Think \ Db \ Driver \ Mysql. class. php.

/*** Execution statement * @ access public * @ param string $ str SQL command * @ return integer | false */public function execute ($ str) {$ this-> initConnect (true); if (! $ This-> _ linkID) return false; $ this-> queryStr = $ str; // release the previous query result if ($ this-> queryID) {$ this-> free ();} N ('db _ write', 1); // record execution start time 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 ;}}

Finally, use the underlying mysql_query to execute the SQL statement.

So far, the source code of setInc has been roughly repeated. I'm sure you know more about how to execute setInc.

The above thinkphp3.2.0 setInc method is a comprehensive analysis of the source code, which is all the content shared by xiaobian. I hope to give you a reference and support for the customer's house.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.