1. Data creation 2. Data write 3. Data read 4. Data Update 5. Data deletion
A Data creation before operations such as database additions, we first need to create the data. What is data creation is the acceptance of the submitted data, such as the POST (default) data submitted by the form. Once the data is accepted, the data can be validated, completed, and generated efficiently.
Create a data Object $user = M (' user ') based on the post data submitted by the form; Var_dump ($user->create ()); PS: Here the Create () method is the data creation, the result of the data is the submitted POST data key value pairs.
It is important to note that the submitted field and the data table fields are corresponding, otherwise they cannot be resolved.
Fetch data manually through an array, overwriting the committed $user = M (' user '); $data [' user '] = $_post[' user ']; $data [' email '] = $_post[' email ']; $data [' date '] = Date (' y-m-d h:i:s ');
corresponding to the data table, otherwise invalid var_dump ($user->create ($data));
Manually fetch the data through the object, overwriting the Committed $user = M (' user '); $data = new \stdclass (); $data->user = $_post[' user '); $data->email = $_post[' email '); $data->date = Date (' y-m-d h:i:s '); Var_dump ($user->create ($data));
The default is $_post, passing $_get Modify $user = M (' user '); Var_dump ($user->create ($_get)); The Create () method can pass the second parameter, and there are two types of modes that will be manipulated: Model::model_insert and model::model_update, which are additions and modifications. When not specified, the system automatically determines whether the data source contains a primary key, or if it contains a primary key, it is the modification operation.
Set the action to be added $user = M (' user '); Var_dump ($user->create ($_post, Model::model_insert));
The internal work of the Create () method is divided into 9 steps:
1. Get the data source (the default is POST), 2. Verify data legitimacy (non-data or objects are filtered), and failure returns FALSE;3. check the field mappings; 4. Determine the status of the data (new or modified); 5. The data is automatically validated, and the failure returns FALSE;6. Form token authentication, failure returns False;7. Form data Assignment (filtering illegal fields and string processing); 8. Data is automatically completed; 9. Generate Data Objects (saved in memory).
The Create () method can be created with consistent operations with data, and the supported coherent operations are:
1.field for defining a valid field; var_dump ($user->field (' user ')->create ()) 2.validate for automatic data validation ; 3.auto for data auto-completion; 4.token for token authentication. Limit the fields that can be manipulated $user = M (' user '); Var_dump ($user->field (' user ')->create ());//In the model class, limit the field class Usermodel extends model {protected $insertFields = ' User '; Protected $updateFields = ' user '; }
Two The data write data is written using the Add () method.
Add a new data $user = M (' user '); $data [' user '] = ' Li Tinghui '; $data [' email '] = ' [email protected] '; $data [' date '] = Date (' y-m-d h:i:s '); $user->add ($data);
Combine the Create () method $user = M (' user '); $data = $user->create (); $data [' date '] = Date (' y-m-d h:i:s '); $user->add ($data); The Add () method supports coherent operations such as:
1.table, define the data table name;
2.data, specifying the data object to write to;
3.field, define the field to write to;
4.relation, correlated query;
5.validate, automatic data verification;
6.auto, data automatic completion;
7.filter, data filtering;
8.scope*, naming range;
9.bind, data binding operation;
10.token, token authentication;
11.comment,sql annotation;//using the data coherence method $user = M (' user '); $data = $user->create (); $data [' date '] = Date (' y-m-d h:i:s '); $user->data ($data)->add (); The data coherence method supports strings, arrays, objects $user = M (' user '); $data = ' user= star vector &[email protected]&date= '. Date (' y-m-d h:i:s '); $user->data ($data)->add ();
Three Data read
In the previous lesson, we had used a lot of data-reading functions, such as the Select () method.
The different requirements for data reading can be achieved by combining various coherent methods, and the methods to support coherence are:
1.where, query or update conditions;
2.table, the name of the data table to manipulate;
3.alias, data table alias;
4.field, query field;
5.order, results sorted;
6.group, query grouping;
7.having, group re-query;
8.join, multi-table links query;
9.union, merge SELECT;
10.distinct, take a unique value;
11.lock, lock;
12.cache, Cache;
13.relation, correlated query;
14.result, data conversion;
15.scope, named range;
16.bind, data binding operation;
17.comment,sql comments.
Displays the default First Data $user = M (' user '); Var_dump ($user->find ());
Can pass digital parameters, AR mode
Show default all data $user = M (' user '); Var_dump ($user->select ());
You can pass an array of SQL//Get the value of the first user field $user = M (' user '); Var_dump ($user->getfield (' user '));
Gets the value of all user fields $user = M (' user '); Var_dump ($user->getfield (' user ', true));
Pass multiple fields to get all $user = M (' user '); Var_dump ($user->getfield (' User,email ')); There's no repetition.
The id colon separates $user = M (' user '); Var_dump ($user->getfield (' Id,user,email ', ': '));
Limit 2 Data $user = M (' user '); Var_dump ($user->getfield (' Id,user,email ', 2));
Four Data Update
The method used to update data is the Save () method, which is primarily a modification of the data.
Modify the First Data $user = M (' user '); $data [' user '] = ' crayon daxin '; $data [' email '] = ' [email protected] '; $map [' id '] = 1; $user->where ($map)->save ($data);
Returns 1 after success, otherwise 0
The default primary key is the condition $user = M (' user '); $data [' id '] = 1; $data [' user '] = ' crayon small new '; $data [' email '] = ' [email protected] '; $user->save ($data);
The supported consistency methods of the Save () method for data updates are:
1.where, query or update conditions;
2.table, the name of the data table to manipulate;
3.alias, data table alias;
4.field, query field;
5.order, results sorted;
6.lock, lock;
7.relation, correlated query;
8.scope, named range;
9.bind, data binding operation;
10.comment,sql comments.
Combine Create () $user = M (' user '); $user->create ();
Post must contain primary key $user->save ();
Modify a value $user = M (' user '); $map [' id '] = 1; $user->where ($map)->setfield (' User ', ' crayon daxin ');
Statistical accumulation, Cumulative $user = M (' user '); $map [' id '] = 1; $user->where ($map)->setinc (' Count ', 1);
Accumulate, Setdec reduce
Five Data deletion
The method used for data deletion is the Delete () method.
Delete primary key directly (id=17) $user = M (' user '); $user->delete (17);
Delete $user by id = M (' user '); $map [' id '] = 16; $user->where ($map)->delete ();
Bulk deletion of multiple $user = M (' user '); $user->delete (' 1,3,5 ');
Delete the first five $user = M (' user ') with a count of 0 and a reverse chronological order; $map [' count '] = 0; $user->where ($map)->order (Array (' date ' = ' DESC '))->limit (5), delete ();
Delete all data, with caution $user = M (' user '); echo $user->where (' 1 ')->delete ();
The delete () method supports the following operations: 1.where, query or update condition, 2.table, data table name to manipulate, 3.alias, data table alias, 4.order, result sort, 5.lock, lock, 6.relation, correlated query; 7. Scope, named range, 8.bind, data binding operation; 9.comment,sql comment.
Six ACTIVEREOCRD mode
The most special mode of this model is to simplify the operation of curd, and adopt the method of object-based operation, which is easy to use and understand.
Add a piece of data $user = M (' user '); $user->user = "Naruto"; $user->email = ' [email protected] '; $user->date = Date (' y-m-d h:i:s '); $user->add ();
Combine Create $user = M (' user '); $user->create (); $user->date = Date (' y-m-d h:i:s '); $user->add ();
Find the value of the primary key 4 $user = M (' user '); Var_dump ($user->find (4));
Find user= Crayon Small new record $user = M (' user '); Var_dump ($user->getbyuser (' Crayon Small New '));
Output user echo $user->user;
Querying multiple $user by primary key = M (' user '); Var_dump ($user->select (' a ') ');
Modify a data $user = M (' user '); $user->find (1); $user->user = ' crayon old new '; $user->save ();
Delete the currently found data $user = M (' user '); $user->find (11); $user->delete ();
Delete data with primary key 10 $user = M (' user '); $user->delete (10);
Delete data with primary key 10,11 $user = M (' user '); $user->delete (' 10,11 ');
Seven Field mappings
field mappings can correspond the name names in the form to the fields in the data table, preventing the system from automatically masking the non-corresponding POST values.
field mappings Protected $_map = Array (' xingming ' = ' user ', ' youxiang ' = ' email ');
Field mappings Get $user = D (' user '); Var_dump ($user->create ());
Thinkphp--curd operation