Curd operation of thinkphp

Source: Internet
Author: User

Curd:create (Create), update (updated), read (read), delete (delete)

1. Data object Creation (CREATE):
A.TP provides a fast creation method for creating data objects that automatically creates objects based on form data, supports arrays, objects, and even creates a data object to a new data object, such as:
$User =m (' User ');
$User->name= ' thinkphp ';
$User->email= ' [email protected] ';
$Member =m (' Member ');
$Member->create ($User);
The Second optional parameter of the B.create method can specify the current data operation state (insert: Model::model_insert or 1; Update: Model::model_update or 2);
By default, the operation status of the data is automatically determined: first the data that requires the operation has the corresponding primary key field in the database, otherwise it will not succeed, if the primary key field value of the data that needs to be manipulated has been saved
In the data table, the system automatically determines the mode of operation is update mode, if the primary key value of the data to be manipulated does not exist in the data table, the system automatically determines that the mode of operation is insert mode.
The data objects created by the C.create method are stored in memory and are not saved to the data table, so we can make changes to the data objects that have been created before the data is saved to the data table, knowing that the
Use the Save or Add method;
D. If we have a custom model class, we can also set the Insertfields and Updatefields properties to define the allowed sub-fields for data additions and edits, such as:
namespace Home\think;
Use Think\model;
Class Usermodel extends model{
protected $insertFields = ' name,email ';//allow writing of name and email fields when new data is added
protected $updateFields = ' email ';//allow writing of email fields when updating data
}
The E.create method is not a coherent operation, and its return value may be a Boolean value and therefore requires strict judgment.

2. Data write: TP mainly provides the Add method and AddAll method;
A.add method slightly;
The B.addall method is primarily used to bulk add data and bulk replace data, such as:
$dataList []=array (' name ' = ' thinkphp ', ' email ' = ' [email protected] ');
$dataList []=array (' name ' = ' onethink ', ' email ' = ' [email protected] ');
$User->addall ($dataList);

3. Data read (Read): Reading data is mainly divided into reading data, data sets, field values;
A. Reading data-find
is actually reading a row of data, mainly through find () to achieve;
If the query fails, the return value is false, if the query value is NULL, the return value is NULL, and if the query succeeds, an associative array (with the field name as the key value) is returned;
Even if there are multiple return values that meet the criteria, find () will always put back only the first one, but the rest of the data can be obtained through data, such as:
$User =m (' User ');
$User->where (' name= ' thinkphp "and Status=1 ')->find ();
Dump ($User->data ());
B. Reading a data set-select
In fact, reading satisfies the conditions of the multi-line record, mainly through the Select () to achieve;
If the query is faulted, the return value is False, the return value is NULL if the query value is NULL, and a two-bit array is returned if the query succeeds.
C. read field values-getfield
In fact, it is to read all the records that meet the criteria of one or several field values, mainly through the GetField () to achieve;
By default, the value of the first row of the data that satisfies the condition is returned, such as: $Users->where (' id=3 ')->getfield (' name ') or $users->getfield (' name ');
You can also get data for the entire column: $Users->getfield (' id ', true);
If you need to get the value of two columns: $Users->getfield (' Name,title '): Returns an associative one-dimensional array, with the corresponding value of the first field to get the key,
The second field to get is the one-dimensional associative array that corresponds to value.
If you need to get the value of three columns: $Users->getfield (' name,title,pid '): Returns a two-dimensional array with the format array (' Namevalue ' =>array (' name ' =>namevalue , ' title ' =>titlevalue, ' pid ' =>pidvalue);
You can also limit the number of records to get: $Users->getfield (' Id,name ', 3);//Get the first 3 records of the query

4. Data update: Data update is divided into updated data and updated fields;
A. Updating Data-save
In fact, is to update an entire record, mainly using save () to achieve;
Arrays and objects are supported: When using an array, the Save method needs to pass in parameters, and when using an object, the Save method does not need to pass in parameters;
If the update fails, the return value is false, and if the update succeeds, the return value is the number of records affected;
For data security, you must include the condition when you update the data, and include the primary key field, or you will not update any database records.
B. Update Fields-setfield
In fact, is to update a field or a number of field values, mainly using SetField to achieve;
Update a field: $Users->where (' id=3 ')->setfield (' name ', ' thinkphp ');
Update several fields: $data =array (' name ' = ' thinkphp ', ' email ' = ' [email protected] ') $Users->where (' id=3 ')->setfield ( $DATA);
For the wanted field, the Setinc method and the Setdec method are provided, such as:
$Users->where (' id=5 ')->setinc (' score ');//score field increased by 1
$Users->where (' id=5 ')->setinc (' Score ', 3);//score field increased by 3
$Users->where (' id=5 ')->setdec (' score ');//score field reduced by 1
$Users->where (' id=5 ')->setdec (' Score ', 3);//score field reduced by 3

5. Deleting data (delete):
In fact, delete one or more of the data, mainly with delete () to achieve;
For data security, use Delete to set the delete condition, otherwise it will not delete any data;
If the delete error occurs, the return value is false, if the data is not deleted, the return value is 0, and if the deletion succeeds, the return value is the number of strips that deleted the record;
You can delete a single piece of data, or you can delete more than one data at a time, depending on the deletion criteria, such as:
$Users->delete (3);//delete record with ID 3
$Users->delete (1,3,5);//delete records with ID 1,3,5 respectively.



Add:
What are the similarities and differences between the 1.data method and the Add method?
A. Both work by creating data objects, but data is easier to use, and create can implement many complex operations when creating data objects, such as field, which defines the legitimacy of the fields.
Validate (Automatic data validation), auto (automatic data completion) and token (token verification)
B.data supports strings, arrays, pairs of thinking; Create supports arrays and objects;

What is the difference between the 2.add method and the Save method?
The Add method is to add data directly to the database, and the Save method updates the data to the database with the primary key field as the criterion.

Curd operation of thinkphp

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.