ThinkPHP has built-in automatic data object filling function, which can be used to process default values, data filtering, and automatic processing of fields written by other systems. Automatic ThinkPHP filling
ThinkPHP
The built-in automatic data object filling function can be used to process default values, data filtering, and automatic processing of fields written by other systems.
To use the auto-fill function, you only need
Defines the $ _ auto attribute (an array composed of multiple verification factors ). The $ _ auto attribute is an array composed of multiple fill factors. the syntax format is as follows:
- Protected $ _ auto = array (
- Array (fill field, fill content [, fill condition] [, add rules])
- };
Description of automatic ThinkPHP filling format: fill factor
Description
The filling field is required. The form field to be filled in. This field is not necessarily a database field, but also some auxiliary fields of the form, such as the verification code.
Filling content
Required. Content to be automatically filled in the field.
The filling condition is optional. Including:
Model: MODEL_INSERT or
Fill in when adding data (default)
Model: MODEL_UPDATE or fill in when updating data
Model: MODEL_BOTH or
Fill in all cases
Optional. Including:
String: string, indicating that the filled content is a string (default)
Function: use a function to indicate that the filled content is a function return value.
Callback: indicates that the filled content is a current Model.
Method return value
Field: field, indicating that the filled content is the value of another field.
Example of automatic filling
Examples of automatic filling that users may use when registering or modifying materials:
- Class UserModel extends Model {
- Protected $ _ auto = array (
- // Set the status field to 1 when adding a new one.
- Array ('status', '1 '),
- // Use the md5 function to process the password field in all circumstances
- Array ('password', 'md5', 3, 'Function '),
- // Call back the getName method when adding the username field
- Array ('username', 'getname', 1, 'callback '),
- // Write the current timestamp to the newly added regdate field
- Array ('regdate', 'time', 1, 'Function '),
- // Write the user-registered IP address to the regip field when it is added.
- Array ('regip', 'get _ client_ip ', 1, 'Function '),
- );
- }
Like automatic verification, the automatic completion mechanism requires the use of create
Method to take effect:
- $ Article = D ("User ");
- If (! $ User-> create ()){
- // If the data object fails to be created (the verification may fail), an error message is displayed.
- Exit ($ Article-> getError ());
- } Else {
- // Continue the next step, such as writing data to the data table
- }
Prompt
Unlike automatic verification, automatic filling is ineffective (for example, calling a nonexistent function or a field that is automatically filled does not exist) and does not cause the creation of a data object (create () to fail, you can only check whether automatic filling is correct and valid through debugging or actual data warehouse receiving.
Automatic completion rules for dynamic changes
Same as automatic verification, it can be used in the operation method
The setProperty method dynamically changes the automatically completed rules:
- $ Dao = D ("User ");
- $ Auto = array (
- // Only process the password field
- Array ('password', 'md5', 1, 'Function ')
- );
- $ User-> setProperty ("_ auto", $ auto );
-
- If (! $ User-> create ()){
- ......
- }
For more information, see ThinkPHP.
Dynamic change verification rules for automatic verification.
Use the auto-fill function in the M method
See ThinkPHP
Automatic verification and filling are implemented when the M method is used (no model class is created).
Make full use of the powerful ThinkPHP
The automatic filling (automatic completion) function allows you to quickly and easily build a form to store data into the database, and the program structure will be clearer.