Thinkphp provides flexible and convenient data manipulation methods, four basic operations for database operations (CURD): Create, update, read and delete the implementation is the most basic, but also must grasp, on this basis to become familiar with more practical data manipulation methods.
Curd operations are usually done in conjunction with a coherent operation.
Data creation
Before data manipulation, we often need to manually create the required data, such as the submitted form data:
// get the Post data for the form $data $_post [' name ']; $data $_post [' email ']; // more form data values to get //...
Creating Data Objects
Thinkphp can help you create data objects quickly, and the most typical application is to automatically create data objects based on form data, which is especially noticeable in the case of a very high number of fields in a single data table.
A very simple example:
// instantiating the user model $User = M (' User '); // create a data object based on the post data submitted by the form $User->create ();
The Create method supports the creation of data objects in other ways, such as from other data objects, or arrays, etc.
$data [' name '] = ' thinkphp '; $data [' email '] = ' [email protected] '; $User->create ($data);
You can even support the creation of new data objects from objects
// Create a new member data object from the user data object $User = stdClass (); $User->name = ' thinkphp '; $User->email = ' [email protected] '; $Member = M ("Member"); $Member->create ($User);
The created data can be read and modified directly, for example:
$data[' name '] = ' thinkphp ';$data[' email '] = ' [email protected] ';$User->create ($data);//data can be read directly after creating the completed data ObjectEcho $User-name;Echo $User-email;//You can also directly modify the data that was created$User->name = ' Onethink ';//Modify the Name field data$User->status = 1;//Add new field data
Data Operation status
The second parameter of the Create method specifies the state of the operation that creates the data, which is automatically determined by default as a write or update operation.
You can also explicitly specify an operation state, for example:
$Member = M ("User"); // Specify update data operation status $Member->create ($_post, model::model_update);
The data operations built into the system include Model::model_insert (or 1) and model::model_update (or 2), and when not specified, the system automatically determines whether the data source contains primary key data, or if there is primary key data, as MODEL: Model_update operation.
Different data operation states can define different data validation and auto-completion mechanisms, so you can customize the state of the data operation you need, for example, you can set the data status of the login operation (assuming 3):
$Member = M ("User"); // Specify update data operation status $Member->create ($_post, 3);
In fact, the work of the Create method is far from simple, and while the creation of the data object completes a series of work, we look at the workflow of the Create method to understand:
Steps |
Description |
Return |
1 |
Get data Source (default is post array) |
|
2 |
Verifying the legitimacy of the data source (non-arrays or objects are filtered) |
The failure returns false |
3 |
Check field mappings |
|
4 |
Judging data status (new or edited, specified or automatically judged) |
|
5 |
Automatic data validation |
The failure returns false |
6 |
Form token validation |
The failure returns false |
7 |
form data Assignment (filtering illegal fields and string handling) |
|
8 |
Data Auto-complete |
|
9 |
Generate Data Objects (saved in memory) |
|
As a result, we are familiar with token validation, auto-validation, and AutoComplete, which must be done through the Create method.
If automatic validation is not defined, the return value of the Create method is an array of data objects that have been created, such as:
$data [' name '] = ' thinkphp '; $data [' email '] = ' [email protected] '; $data [' status '] = 1; $User = M (' User '); $data $User->create ($data);d UMP ($data);
The output is:
Array (size=3) string ' thinkphp ' (length=8) string ' [email protected] ' (length=18) ' status ' + int 1
The data object created by the Create method is stored in memory and is not actually written to the database until it is actually written to the database using the Add or Save method.
So before we call the Add or save method, we can change the data object created by the Create method, for example:
$User = M (' User '); $User // creating a User data Object $User // Set the default user state $User Time // set the user's creation time $User // writing user objects to the database
If you want to simply create a data object and do not need to complete some additional functionality, you can use the data method to create a simple object. Use the following:
// instantiating the user model $User = M (' User '); // write to database after creating data $data [' name '] = ' thinkphp '; $data [' email '] = ' [email protected] '; $User->data ($data)->add ();
The data method also supports the passing of arrays and objects, which are not automatically validated and filtered by objects that are created using the database method, and should be handled on their own. However, in the case of add or save operations, fields that do not exist in the data table and illegal data types (such as objects, arrays, and other non-scalar data) are automatically filtered, without worrying about SQL errors caused by non-table field writes.
Supported, consistent operations
The consistent operation methods supported by the Create method include:
Consistent operation |
Role |
Supported parameter types |
Field |
Used to define a valid field |
Strings and Arrays |
Validate |
For automatic data validation |
Array |
Auto |
For data Auto-completion |
Array |
Token |
For token validation |
Boolean value |
field legality filtering
If you call the field method before the Create method, it means that only the specified field data is allowed, and other illegal fields will be filtered, for example:
$data [' name '] = ' thinkphp '; $data [' email '] = ' [email protected] '; $data [' status '] = 1; $data [' test '] = ' test '; $User = M (' User '); $data $User->field (' Name,email ')->create ($data);d UMP ($data);
The output is:
Array (size=2) string ' thinkphp ' (length=8) string ' [email protected] ' (length=18)
Eventually only the name and email fields are allowed to be written, and the status and test fields are filtered directly, even if status is a valid field in the datasheet.
If we have a custom model class, we can also define the allowed fields directly within the model class by setting the Insertfields and Updatefields properties for data additions and edits, for example:
namespace Home\model; Use Think\model; class extends model{ protected$insertFields// new data allowed to write name and email fields protected$updateFields// edit data is only allowed when writing to the email field }
ThinkPHP3.2 Basic Tutorial (19)--model-CURD operations-data creation