Thinkphp automatically completes data verification Usage Details (suitable for beginners)

Source: Internet
Author: User
Provides various official and user-released code examples. For more information, see thinkphp.
Automatic completion is a method provided by thinkphp to automatically process and filter data. When you use the create method to create a data object, data processing is automatically completed.
Therefore, it is safer to use the create method in thinkphp to create data objects, rather than directly writing data through the add or save method.

Reproduced original address: http://www.jb100.net/html/content-28-481-1.html

Rule Definition

Automatic completion is usually used to write default fields, filter security fields, and automatically process business logic. It is similar to the definition method of automatic verification, automatic definition also supports static definition and dynamic definition.
Static mode: Define processing rules using the $ _ auto attribute in the model class.
Dynamic Mode: Use the auto method of the model class to dynamically create automatic processing rules.
Both methods use the following rules:
Complete field: (required) Name of the actual field of the data table to be processed.
Completion rules: (required) the rules to be processed must be followed by additional rules.
Completion Time: (optional) includes:
Model: process data added when MODEL_INSERT or 1 is added (default)
Model: MODEL_UPDATE or 2 processing when updating data
Model: MODEL_BOTH or 3. All situations are processed.
Additional rules: (optional) includes:
Function indicates that the filled content is a function name.
Callback method, indicating that the filled content is a method of the current Model
Field is filled with other fields, indicating that the filled content is the value of another field.
String (default)

Array (
Array (completion Field 1, completion rule, [Completion condition, add rule]),
Array (complete Field 2, complete rule, [Completion condition, add rule]),
......
);

Static Definition

We define automatic rules in the Model class in advance, which is called static definition. For example, we define the _ auto attribute in the model class: Then we can automatically process the data object when using the create method: if you do not define any automatic verification rules, you do not need to determine the return value of the create method: or use the create method to generate data objects based on the post data submitted by the form by default, we can also generate Data Objects Based on other data sources. You can also specify whether to add or edit data when the currently created data objects are automatically processed. For example: the second parameter of the create method is used to specify the completion time of the Automatic completion rule. That is to say, the automatic processing rule of the create method only processes the Automatic completion rule that meets the completion time.
When creating data, the create method has automatically filtered out non-data table field data information. Therefore, you do not need to worry that the form will submit other illegal field information, resulting in an error in writing data objects, you can even automatically filter fields that you do not want to submit in the form (for details, see Field validity filtering ).

The Code is as follows: Class UserModel extends Model {
Protected $ _ auto = array (
Array ('status', '1'), // set the status field to 1 when adding a new value.
// Process the md5 function when adding and editing the password field
Array ('Password', 'md5', 3, 'function '),
// Call back the getName method when adding or editing the name field
Array ('name', 'getname', 3, 'callback '),
// Write the current timestamp to the update_time field during update
Array ('Update _ time', 'time', 2, 'function '),
);
}

$ User = D ("User"); // instantiate the User object
If (! $ User-> create () {// create a Data Object
// If creation fails, the verification fails and an error message is displayed.
Exit ($ User-> getError ());
} Else {
// Verify that new data is written
$ User-> add ();
}

$ User = D ("User"); // instantiate the User object
$ User-> create (); // generate a Data Object
$ User-> add (); // add User Data

$ User = D ("User"); // instantiate the User object
$ User-> create ()-> add (); // generate a data object and write data

$ User = D ("User"); // instantiate the User object
$ UserData = getUserData (); // obtain user data through a method
// Create a data object based on userData and define it as updating data
$ User-> create ($ userData, 2 );
$ User-> add ();
Dynamic completion

In addition to static definitions, different processing rules can also be solved dynamically.

The Code is as follows: $ Rules = array (
// Set the status field to 1 when adding a new one.
Array ('status', '1 '),
// Process the md5 function when adding and editing the password field
Array ('Password', 'md5', 3, 'function '),
// Write the current timestamp to the update_time field during update
Array ('Update _ time', 'time', 2, 'function '),
);
$ User = M ('user ');
$ User-> auto ($ rules)-> create ()-> add ();
Modify Data Objects

After the data object is created using the create method, the data object is stored in the memory, so you can still operate the data object, including modifying or adding the value of the data object, for example: once the add method (or save method) is called, the data object created in the memory will become invalid. If you want to create a data object to be called again in subsequent data processing, you can save data objects first. For example, if you modify the data objects in the memory, the stored data objects are not automatically updated. Therefore, the following usage is incorrect: the above code modifies the data object, but still writes the previously saved data object, so the change operation on the data object will be invalid.

The Code is as follows: $ User = D ("User"); // instantiate the User object
$ User-> create (); // generate a Data Object
$ User-> status = 2; // modify the status attribute of the Data Object
$ User-> register_time = NOW_TIME; // Add the register_time attribute.
$ User-> add (); // add User Data
$ User = D ("User"); // instantiate the User object
$ Data = $ User-> create (); // Save the generated data object
$ User-> add ();
$ User = D ("User"); // instantiate the User object
$ Data = $ User-> create (); // Save the generated data object
$ User-> status = 2; // modify the status attribute of the Data Object
$ User-> register_time = NOW_TIME; // Add the register_time attribute.
$ User-> add ($ data );
Reproduced original address: http://www.jb100.net/html/content-28-481-1.html

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.