thinkphp, deleting, changing and checking the

Source: Internet
Author: User

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");//instantiating a User object
$data ['name'] ='thinkphp';
$data ['Email'] ='[email protected]';
$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");//Instantiating a 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");//instantiating a User object
//Find user data with status value 1 to create time sort return 10 data
$list = $Userwhere('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");//instantiating a User object
//look for user data with a status value of 1name value think
$Userwhere('Status=1 and Name= "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");//instantiating a User object
//get the nickname for the user with ID 3
$nickname = $Userwhere('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");//instantiating a User object
//get the ID and nickname list for 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 the Save method in thinkphp to update the database, and also support the use of coherent operations.
$User = M ("User");//instantiating a User object
//assignment of data object properties to modify
$data ['name'] ='thinkphp';
$data ['Email'] ='[email protected]';
$Userwhere('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, the Save method does not update the records of any database.
Therefore, the following code does not change any records in the database
$User->save ($data);
Unless you use the following method:
$User = M ("User");//Instantiating a User object
//assignment of data object properties to modify
$data ['ID'] =5;
$data ['name'] ='thinkphp';
$data ['Email'] ='[email protected]';
$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 parameters of the Save method do not need to be passed in.
$User = M ("User");//Instantiating a User object
//assignment of data object properties to modify
$data ['name'] ='thinkphp';
$data ['Email'] ='[email protected]';
$Userwhere('id=5')->data ($data)->save ();//save modified data based on conditions
Examples of using the Create method:
$User = M ("User");//instantiating a 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");//instantiating a User object
//change the user's name value
$Userwhere('id=5')->setfield ('name','thinkphp');
The SetField method supports updating multiple fields at the same time, requiring only incoming arrays, such as:
$User = M ("User");//instantiating a User object
//change the value of the user's name and email
$Userwhere('id=5')->setfield (Array ('name','Email'), Array ('thinkphp','[email protected]'));
The system also provides Setinc and Setdec methods for updating statistical fields, which are typically numeric types:
$User = M ("User");//instantiating a User object
$User->setinc ('score','id=5',3);//users ' points plus 3
$User->setinc ('score','id=5');//users ' points plus 1
$User->setdec ('score','id=5',5);//User's points minus 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");//instantiating a User object
$Userwhere('id=5')->delete ();//Delete user data with ID 5
$Userwhere('status=0')->delete ();//Delete all user data with a status of 0
The Delete method can be used to delete a single or multiple data, depending on the delete condition, which is the parameter of the Where method, or the order and limit methods to limit the number of deletions, for example:
//Delete all 5 user data with a status of 0 sorted by creation time
$Userwhere('status=0')->order ('Create_time')->limit ('5')->delete ();

thinkphp, deleting, changing and checking the

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.