The data 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 perhaps we are not accustomed to using this method today to explain how to use the good data method.
Usage
Write operations
$Model = D (' User ');
$Model->create ();
//Here to skip the specific automatic generation and verification of the Judgment
$Model->add ();
or assign a value directly to the data object, for example:
$Model = M (' User ');
$Model->name = ' fleeting ';
$Model->email = ' [email protected] ';
$Model->add ();
The data method, then, is to directly generate the object to manipulate, for example:
$Model = M (' User ');
$data [' name '] = ' fleeting ';
$data [' email '] = ' [email protected] ';
$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 = ' [email protected] ';
$Model->data ($obj)->add ();
Here's how to use strings:
$Model = M (' User ');
$data = ' name= fleeting &[email protected] ';
$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 '] = ' [email protected] ';
$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 '] = ' [email protected] ';
$Model->data ($data)->save ();
Of course we can also use this directly:
$Model = M (' User ');
$data [' id '] = 8;
$data [' name '] = ' fleeting ';
$data [' email '] = ' [email protected] ';
$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 '] = ' [email protected] ';
$Model->data ($data)->where (' id=8 ')->save ();
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 ();
Data method in thinkphp