Some summaries on the increase, deletion, modification and investigation of thinkphp

Source: Internet
Author: User
today learned the thinkphp of the increase, delete, change, check some operations, feel written quite clear, we learn together!

First, create an action

Use the Add method in thinkphp to add data to the database.

Here's how to use it:

$User = M ("User"); Instantiate the User object $data[' name '] = ' thinkphp '; $data [' email '] = ' ThinkPHP@gmail.com '; $User->add ($data);

Or use the data method to operate coherently

$User->data ($data)->add ();

If a data object has been created before add (for example, using the Create or data method), the Add method does not need to pass in the data again.

Examples of using the Create method:

$User = M ("User"); Instantiate the User object//Create a data object based on the post data submitted by the form $user->create (); $User->add (); Save modified data based on conditions

If your primary key is an autogrow type, and if the insert data succeeds, the return value of the Add method is the most recently inserted primary key value, which can be obtained directly.

Second, read the data

There are many ways to read data in thinkphp, usually divided into read data and read data sets.

Read datasets using FindAll or Select methods (FindAll and select methods are equivalent):

$User = M ("User"); Instantiate the User object//Find the status value of 1 to create a time sort to return 10 data $list = $User->where (' Status=1 ')->order (' Create_time ') Limit (Ten)->select ();

The return value of the Select method is a two-dimensional array that returns an empty array if no results are queried. Complex data queries can be accomplished with the above-mentioned coherent operations. And the most complex coherent method should be The use of the Where method, because this part involves a lot of content, we will be in the Query Language section on how to make the assembly query conditions for detailed usage instructions. The basic query does not involve the associated query section at the moment, but unifies the correlation model for data manipulation, which is part of the correlation model section.

To read data using the Find method:

The operation of reading data is similar to a dataset, and all of the consistent operation methods available for select can be used in the Find method, except that the Find method returns only one record, so the limit method is not valid for the Find query operation.

$User = M ("User"); Instantiate the User object//Find the $user->where (' Status=1 and name= ' think ') with a status value of 1name value of Think ()->find ();

The Find method returns only the first record, even if there is more than one piece of data that satisfies the condition.

If you want to read the value of a field, you can use the GetField method, for example:

$User = M ("User"); Instantiate the User object//Get the nickname for the username ID 3 $nickname = $User->where (' id=3 ')->getfield (' nickname ');

When there is only one field, a value is always returned.

If you pass in more than one field, you can return an associative array:

$User = M ("User"); Instantiate the User object//Get the ID and nickname list of all users $list = $User->getfield (' id,nickname ');

The returned list is an array, the key name is the user's ID, and the key value is the user's nickname nickname.

Third, update the data

use Save in thinkphp method updates the database and also supports the use of coherent operations.

$User = M ("User"); Instantiate the User object//To modify the data object property assignment $data[' name '] = ' thinkphp '; $data [' email '] = ' ThinkPHP@gmail.com '; $User->where (' id=5 ')- >save ($data); Save modified data based on conditions

in order to ensure the security of the database, to avoid errors update the entire data table, if there is no update condition, the data object itself does not contain the primary key field, save method does not update records for any database.

So the following code does not change any records in the database

$User ->save ($data);

Unless you use the following method:

$User = M ("User"); Instantiate the User object//Assign value to the data object property to be modified

$data [' id '] = 5; $data [' name '] = ' thinkphp '; $data [' email '] = ' ThinkPHP@gmail.com '; $User->save ($data); Save modified data based on conditions

If the ID is the primary key of the data table, the system automatically updates the value of the other field with the value of the primary key as the update condition.

another way to do this is to create the data object to be updated by creating or by data method, and then save the operation so that the save the parameters of the method may not need to be passed in.

$User = M ("User"); Instantiate the User object//To modify the data object property assignment $data[' name '] = ' thinkphp '; $data [' email '] = ' ThinkPHP@gmail.com '; $User->where (' id=5 ')- >data ($data)->save (); Save modified data based on conditions

Examples of using the Create method:

$User = M ("User"); Instantiate the User object//Create a data object based on the post data submitted by the form $user->create (); $User->save (); Save the data you want to modify according to the criteria

In the above case, the form must contain a hidden field with a primary key name in order to complete the save operation.

If you just update the values of individual fields, you can use the SetField Method:

$User = M ("User"); Instantiate the User object//change the name value of the $user-> where (' id=5 ')->setfield (' name ', ' thinkphp '); the SetField method supports updating multiple fields at the same time, just passing in the array. For example: $User = M ("User"); Instantiate the User object//change the value of the name and email of the $user-> where (' id=5 ')->setfield (' name ', ' email '), array (' thinkphp ', ' ThinkPHP@gmail.com '));

for updates to statistical fields (usually numeric types), the system also provides Setinc and the Setdec Method:

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

Iv. deletion of data

Use the Delete method in thinkphp to delete records in the database. You can also use a consistent operation for delete operations.

$User = M ("User"); Instantiate the User object $user->where (' id=5 ')->delete (); Delete user data with ID 5 $user->where (' status=0 ')->delete (); Delete all user data with a status of 0

Delete method can be used to delete a single or multiple data, mainly depending on the deletion condition, i.e. where parameter of the method, or you can use the Order and the Limit method to limit the number of deletions to be deleted, for example:

Delete all 5 user data with a status of 0 sorted by creation time $user->where (' status=0 ')->order (' Create_time ')->limit (' 5 ')->delete ();

This article explains about the thinkphp of the increase, deletion, change, check some summary, more relevant content please pay attention to the PHP Chinese web.

Related recommendations:

Application of where Method explained

Thinkphp the contents of the double loop traversal output

Introduction to ThinkPHP5 Quick Start method

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.