Sometimes, we hope that the system can help us automate some functions, such as automatic password encryption, ignoring the empty, we need to take advantage of the automatic completion (fill) function.
The data processing methods provided by the Thinkphp model layer are mainly used for automatic processing and filtering of data, and creating data using the Create () method is done automatically. Automatic completion is typically done through default field writes, safe field filtering, and business logic
Automatic processing and so on. There are two ways to implement auto-complete rules: 1. Static mode: Define processing rules by $_auto attribute in model class; 2 dynamic mode: Use the Auto method of the model class to dynamically create automatic processing rules.
I. Completion of the Rules
Array
Array (Complete field 1, complete rule 1,[Complete Rule 1, complete condition 1]),
Array (Complete field 2, complete rule 2,[Complete Rule 2, complete condition 2]),
)
Completion conditions: Optional, as follows:
1.self::model_insert or 1, when new data is processed (default);
2.self::model_update or 2, update the data when processing;
3.self::model_both or 3, all cases are handled.
Additional rules: optional, with the use of complete rules, including the rules:
Rule description
function is complete, the validation rule defined is a function name
callback method completion, defined validation rule is a method of the current model class
field is populated with other fields, indicating that the content of the fill is the value of a different field
String strings (Default)
Ignore NULL is ignored
Two. Static mode
Under the Common/usermodel:
Class Usermodel extends model{ protected $_auto = Array ( //new When setting the Status field to 1 array (' status ', ' 1 '), / /To the password field in all cases use the MD5 function to process the array (' Password ', ' MD5 ', 3, ' function '), //to the Username field in the new callback GetName method Array (' username ', ' getName ', 1, ' callback '), //To the RegDate field when added to the current timestamp array (' regdate ', ' time ', 1, ' function ') ,//The REGIP field is added when the user registers the IP address of the array (' Regip ', ' get_client_ip ', 1, ' function ') ;}
Bound to home/controller/usercontroller.class.php
Three. Dynamic mode
Write directly in the home/controller/usercontroller.class.php, that is, dynamic completion is the completion of the rules on the controller side, so that in the operation of the time is more flexible, the disadvantage is more than
More chaotic.
Dynamic completion
$rules = Array (
Array (' user ', ' SHA1 ', 3, ' function '),//complete encryption of the user field
);
$user = M (' user ');
$data [' user '] = ' crayon small new ';
if ($user->auto ($rules)->create ($data)) {
$user->add ();
}
Realization of automatic completion of thinkphp3.2.3