1. After the data is removed, change the contents of the field to update the data
$user = user::get (1); $user->name = 'thinkphp'; $user->email = '[email protected]'; $user->save ();
2. Update data directly with update conditions
$user New User; // Save Method The second parameter is the update condition $user-Save ([ ' name ' = ' thinkphp ', ' email ' = ' [email protected] '] , [' id ' = 1]);
3. Filtering data from non-data table fields
$user New User (); // Filter Non-data table field data in the post array $user->allowfield (true)->save ($_post, [' id ' = 1]);
4. Assign a value to the model by an external commit, and you want to specify some fields to write to
$user New User (); // only the name and email fields in the post array will be written to $user->allowfield ([' Name ', ' email '])->save ($_post, [' id ' = 1]);
5, SaveAll method batch update data
$user New User; $list = [ ' id ' =>1, ' name ' = ' thinkphp ', ' email ' = ' [email protected] ', [' id ' =>2, ' name ' = ' onethink ', ' email ' = ' [email protected] ']; $user->saveall ($list);
7. Iterate through batch update data
$user New User; $list = [ ' id ' =>1, ' name ' = ' thinkphp ', ' email ' = ' [email protected] ', [ ' id ' =>2, ' name ' = ' onethink ', ' email ' = ' [email protected] ']; foreach ($listas$data) { $user->data ($data, true) ->isupdate (true),Save ();}
8. Update data by database class (two forms)
$user New User; $user->where (' id ', 1) ->update ([' name ' = ' thinkphp ']); $user New User; $user->update ([' id ' = 1, ' name ' = ' thinkphp ');
9, static method directly update data (two forms)
// The first form of User::where(' id ', 1),update([' name ' = ' thinkphp ']); // The second form of User::update([' id ' = + 1, ' name ' = ' thinkphp ')];
10. The closure function uses more complex update conditions
$user New User; $user->save ([' name ' = ' thinkphp '],function($query) { // Update data with a status value of 1 and an ID greater than 10 $query->where (' status ', 1)->where (' id ', ' > ', ');});
11. Automatic identification of primary key updates
// instantiating a model $user New User; // explicitly specifying an update data operation $user->isupdate (true)->save ([' id ' = 1, ' name ' = ' thinkphp ']);
ThinkPHP5.0 Model Update Operations