TP5 Database Operations

Source: Internet
Author: User
The operational database is the foundation of the project operation, and TP5 provides us with a very convenient method of operation. This article has shared the TP5 operation database method, through the example carries on the explanation, hoped to be helpful to everybody.

SetField updating the value of a field

1

    $User = M ("User"); Instantiate    the User object//change the name value of the users    $User, where (' id=5 ')->setfield (' name ', ' thinkphp ');

The 2 SetField method supports updating multiple fields at the same time, requiring only incoming arrays, such as:

      $User = M ("User"); Instantiate the User object    //Change the value of the name and email of the users    $data = Array (' name ' = ' thinkphp ', ' email ' = ' ThinkPHP@gmail.com ');    $User, where (' id=5 ')->setfield ($data);

The system also provides Setinc and Setdec methods for updating statistical fields, which are often referred to as numeric types.

    $User = M ("User"); Instantiate the User Object    $User->where (' id=5 ')->setinc (' Score ', 3);//users ' points plus 3    $User->where (' id=5 ')->setinc (' score '); The user's points plus 1    $User->where (' id=5 ')->setdec (' Score ', 5);//users ' points minus 5    $User->where (' id=5 ')->setdec (' Score '); User's points minus 1

Updating data in a data table

    Db::table (' Think_user ')->where (' id ', 1)->update ([' name ' = ' thinkphp ']);

If the data contains a primary key, you can use it directly:

    Db::table (' Think_user ')->update ([' name ' = ' = ' thinkphp ', ' ID ' =>1]);

The Update method returns the number of bars that affect the data, without modifying any data to return 0

If the data you want to update needs to use SQL functions or other fields, you can use the following method:

    Db::table (' Think_user ')        ->where (' id ', 1)        ->update (['            login_time '  = = [' exp ', ' Now () '],            ' login_times ' = [' exp ', ' login_times+1 '],        ];

Update the value of a field:

    Db::table (' Think_user ')->where (' id ', 1)->setfield (' name ', ' thinkphp ');

The SetField method returns the number of bars that affect the data, without modifying any data fields returned 0

Increment or decrement the value of a field

    Setinc/setdec If the second parameter is not added, the default value is 1    //score field plus 1    db::table (' Think_user ')->where (' id ', 1)->setinc (' score ') ;    Score field plus 5    db::table (' Think_user ')->where (' id ', 1)->setinc (' Score ', 5);    Score field minus 1    db::table (' Think_user ')->where (' id ', 1)->setdec (' score ');    Score field minus 5    db::table (' Think_user ')->where (' id ', 1)->setdec (' Score ', 5);

Deferred updates

SETINC/SETDEC Support delay Update, if need delay update pass the third parameter

The following example delays 10 seconds and adds 1 to the score field

    Db::table (' Think_user ')->where (' id ', 1)->setinc (' Score ', 1, 10);

Setinc/setdec method returns the number of bars that affect the data

Helper functions

    Update data in data table    db (' user ')->where (' id ', 1)->update ([' name ' = = ' thinkphp ']);    Update the value of a field    db (' user ')->where (' id ', 1)->setfield (' name ', ' thinkphp ');    Self-increment score field    db (' user ')->where (' id ', 1)->setinc (' score ');    Self-decrement score field    db (' user ')->where (' id ', 1)->setdec (' score ');

Quick Update (v5.0.5+)

The data, Inc, DEC, and Exp methods encapsulated in v5.0.5+ and above are chain-operated methods that can be used with update.

Here is an example of how to use:

    Db::table (' data ')        ->where (' id ', 1)        ->inc (' read ')        ->dec (' score ', 3)        ->exp (' name ', ' UPPER (name) ')        ->update ();

Related recommendations:

TP5 Auth Rights Management instance

TP5 Automatic loading mechanism detailed

TP5 Model Function Summary

Related Article

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.