ThinkPHP3.1 quick start (13) automatically completed

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

Automatic rule definition 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:
  1. Array (
  2. Array (completion Field 1, completion rule, [completion condition, add rule]),
  3. Array (complete field 2, complete rule, [completion condition, add rule]),
  4. ......
  5. );
Copy code description
Complete field(Required) name of the actual field of the data table to be processed.
Rule completion(Required) the rules to be processed must be added.
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 Use a function to indicate that the filled content is a function name.
Callback Callback method, indicating that the filled content is a method of the current model
Field Fill with other fields to indicate that the filled content is the value of another field.
String String (default)
Ignore If it is null, ignore (3.1.2 New)

Static definition defines automatic completion rules in the model class in advance, which is called static definition. For example, we define the _ auto attribute in the model class:
  1. Class UserModel extends Model {
  2. Protected $ _ auto = array (
  3. Array ('status', '1'), // Set the status field to 1 when adding a new value.
  4. Array ('password', 'md5', 3, 'Function'), // use the md5 function to process the password field when it is added or edited.
  5. Array ('name', 'getname', 3, 'callback'), // calls back the getName method when adding or editing the name field.
  6. Array ('update _ time', 'time', 2, 'Function'), // write the current timestamp to the update_time field during update
  7. );
  8. }
Copy the code and then use the create method to automatically process the data object:
  1. $ User = D ("User"); // instantiate the User object
  2. If (! $ User-> create () {// create a data object
  3. // If creation fails, the verification fails and an error message is displayed.
  4. Exit ($ User-> getError ());
  5. } Else {
  6. // Verify that new data is written
  7. $ User-> add ();
  8. }
If you do not define any automatic verification rules, you do not need to judge the return value of the create method:
  1. $ User = D ("User"); // instantiate the User object
  2. $ User-> create (); // Generate a data object
  3. $ User-> add (); // add User data
Copy the code or use it more easily:
  1. $ User = D ("User"); // instantiate the User object
  2. $ User-> create ()-> add (); // Generate a data object and write data
By default, the copy code create method generates data objects based on the post data submitted by the form. we can also generate data objects based on other data sources, you can also specify whether to add or edit data when the data object is automatically processed. for example:
  1. $ User = D ("User"); // instantiate the User object
  2. $ UserData = getUserData (); // obtain user data through a method
  3. $ User-> create ($ userData, 2); // create a data object based on userData and define it as updating data
  4. $ User-> add ();
The second parameter of the Copy code create method is used to specify the completion time in 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 ).

In version 3.1.2, the ignore completion rule is added. this rule indicates that a field is ignored if it is left blank. it can be used to enter the password when modifying user data. The definition is as follows:
  1. Array ('password', '', 2, 'ignore ')
Copy the code to ignore this field if it is left blank during the password field editing.

In addition to static definition, dynamic completion can also be used to solve different processing rules.
  1. $ Rules = array (
  2. Array ('status', '1'), // Set the status field to 1 when adding a new value.
  3. Array ('password', 'md5', 3, 'Function'), // use the md5 function to process the password field when it is added or edited.
  4. Array ('update _ time', 'time', 2, 'Function'), // write the current timestamp to the update_time field during update
  5. );
  6. $ User = M ('user ');
  7. $ User-> auto ($ rules)-> create ()-> add ();
Copy code
After you use the create method to create a data object, the data object is stored in the memory. Therefore, you can still operate on the data object, including modifying or increasing the value of the data object. for example:
  1. $ User = D ("User"); // instantiate the User object
  2. $ User-> create (); // Generate a data object
  3. $ User-> status = 2; // modify the status attribute of the data object
  4. $ User-> register_time = NOW_TIME; // add the register_time attribute.
  5. $ User-> add (); // add User data
Once the add method (or save method) is called, the data object created in the memory becomes invalid. if you want to create a data object to be called again in subsequent data processing, you can save the data object first, for example:
  1. $ User = D ("User"); // instantiate the User object
  2. $ Data = $ User-> create (); // Save the generated data object
  3. $ User-> add ();
Copy the code but remember that if you modify the data objects in the memory, the stored data objects are not automatically updated. Therefore, the following usage is incorrect:
  1. $ User = D ("User"); // instantiate the User object
  2. $ Data = $ User-> create (); // Save the generated data object
  3. $ User-> status = 2; // modify the status attribute of the data object
  4. $ User-> register_time = NOW_TIME; // add the register_time attribute.
  5. $ User-> add ($ data );
Copy the code above. We modified the data object, but still write the previously saved data object. Therefore, the change operation on the data object will be invalid.

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.