ThinkPHP5.0 Model Update Operations

Source: Internet
Author: User

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

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.