ThinkPHP auto-fill (auto-complete) details and examples

Source: Internet
Author: User
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:




  1. Protected $ _ auto = array (
  2. Array (fill field, fill content [, fill condition] [, add rules])
  3. };

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:




  1. Class UserModel extends Model {
  2. Protected $ _ auto = array (
  3. // Set the status field to 1 when adding a new one.
  4. Array ('status', '1 '),
  5. // Use the md5 function to process the password field in all circumstances
  6. Array ('password', 'md5', 3, 'Function '),
  7. // Call back the getName method when adding the username field
  8. Array ('username', 'getname', 1, 'callback '),
  9. // Write the current timestamp to the newly added regdate field
  10. Array ('regdate', 'time', 1, 'Function '),
  11. // Write the user-registered IP address to the regip field when it is added.
  12. Array ('regip', 'get _ client_ip ', 1, 'Function '),
  13. );
  14. }

Like automatic verification, the automatic completion mechanism requires the use of create
Method to take effect:




  1. $ Article = D ("User ");
  2. If (! $ User-> create ()){
  3. // If the data object fails to be created (the verification may fail), an error message is displayed.
  4. Exit ($ Article-> getError ());
  5. } Else {
  6. // Continue the next step, such as writing data to the data table
  7. }

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:




  1. $ Dao = D ("User ");
  2. $ Auto = array (
  3. // Only process the password field
  4. Array ('password', 'md5', 1, 'Function ')
  5. );
  6. $ User-> setProperty ("_ auto", $ auto );

  7. If (! $ User-> create ()){
  8. ......
  9. }

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.

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.