The data method of the Thinkphp curd method is also one of the consistent operation methods of the model class, used to set the value of the object currently being manipulated, and many developers are not accustomed to using this method, today to explain how to use the good data method.
The specific usage is as follows:
1. Write operations
Typically, we generate data objects from the Create method or the assignment, and then write to the database, for example:
$Model = D (' User '); $Model->create (); Here to skip the specific automatic generation and verification of Judgment $model->add ();
or assign a value directly to the data object, for example:
$Model = M (' User '), $Model->name = ' fleeting '; $Model->email = ' thinkphp@qq.com '; $Model->add ();
The data method, then, is to directly generate the object to manipulate, for example:
$Model = M (' User '), $data [' name '] = ' fleeting '; $data [' email '] = ' thinkphp@qq.com '; $Model->data ($data)->add ();
Note: If we use both the Create method and the data object, then the method that is called later is valid .
The data method supports arrays, objects, and strings in the following way:
$Model = M (' User '); $obj = new StdClass; $obj->name = ' fleeting '; $obj->email = ' thinkphp@qq.com '; $Model->data ($obj)- >add ();
Here's how to use strings:
$Model = M (' User '), $data = ' name= fleeting &email=thinkphp@qq.com '; $Model->data ($data)->add ();
You can also add data directly to the Add method by passing in the data object, for example:
$Model = M (' User '), $data [' name '] = ' fleeting '; $data [' email '] = ' thinkphp@qq.com '; $Model->add ($data);
But this way the data parameter can only use arrays.
Of course, the data method can also be used to update information, such as:
$Model = M (' User '); $data [' id '] = 8; $data [' name '] = ' fleeting '; $data [' email '] = ' thinkphp@qq.com '; $Model->data ($data) Save ();
Of course we can also use this directly:
$Model = M (' User '); $data [' id '] = 8; $data [' name '] = ' fleeting '; $data [' email '] = ' thinkphp@qq.com '; $Model->save ($data);
Similarly, the data parameter can only pass in the array at this time.
When the Save method is called to update the data, it automatically determines if there is a primary key value in the current data object, and if so, automatically updates the condition. In other words, the following usage is equivalent to the above:
$Model = M (' User '), $data [' name '] = ' fleeting '; $data [' email '] = ' thinkphp@qq.com '; $Model->data ($data)->where (' id=8 ')- >save ();
2. Read operation
In addition to the write operation, the data method can be used to read the current object, for example:
$User = M (' User '), $map [' name '] = ' fleeting '; $User->where ($map)->find (); Reads the current data Object $data = $User->data ();
http://www.bkjia.com/PHPjc/825320.html www.bkjia.com true http://www.bkjia.com/PHPjc/825320.html techarticle the data method of the Thinkphp curd method is also one of the consistent operation methods of the model class, used to set the value of the object currently being manipulated, and many developers are not used to this method ...