1. Automatic completion
Auto-Completion is the method that thinkphp provides to automate data processing and filtering, and data processing is done automatically when the creation method is used to create the object.
Therefore, it is safer to use the Create method for creating data objects in thinkphp, rather than directly using the Add or save methods to write data.
1.1 Auto Completion function
autocomplete is usually used to: ①. Complete Default field Write ②. secure field filter ③. Automatic processing of business logic, etc.
The 1.2 rule defines the static mode: Inside the model class, the processing rule is defined by the $_auto attribute. Dynamic mode: Dynamically creating automatic Processing rules using the Auto method of the model class.
mode:
Array (
Complete field 1, completion rule, [completion condition, additional rules]),
Array (Complete field 2, complete rule, [completion condition, additional rules]),
...
Definition Description: Complete field (required)
The actual field name of the datasheet that needs to be processed. complete the rule (must)
The rules that need to be handled are completed with additional rules. time to finish (optional)
Set the time for automatic completion, including:
Set up |
Description |
Self::model_insert or 1. |
When new data is processed (default) |
Self::model_update or 2. |
Processing when updating data |
Self::model_both or 3. |
All the cases are handled |
additional rules (optional)
Including:
rules |
Description |
function |
Use a function to indicate that the content of a fill is a function name |
Callback |
callback method that indicates that the content of the fill is a method of the current model |
Field |
Fill with a different field to indicate that the content of the fill is a value of another field |
String |
String (default) |
Ignore |
Is null to ignore |
1.3 Statically define an auto-complete validation rule
Registered users
namespace Home\model;
Use Think\model;
Class Usermodel extends model{
protected $_auto = Array (
' status ', ' 1 '), set the status field to 1 when new
Array (' Password ', ' MD5 ', 3, ' function '),//The password field is added and edited so that the MD5 function handles
array (' name ', ' GetName ', 3, ' callback '),/ /The Name field callback GetName method
Array (' Update_time ', ' time ', 2, ' function ') when new and edited,//to write the current timestamp to the Update_time field at the time of the update
;
}
You can then automatically process the data object when you create it using the Create method:
$User = D ("User"); Instantiate the User object
if (! $User->create ()) {//Create a data Object
//////If the creation fails indicating that the validation did not pass the output error message
exit ($User->geterror () );
} else{
//Validation
$User->add () by writing new data;
If you do not define any automatic validation rules, you do not need to determine the return value of the Create method:
$User = D ("User"); Instantiate the User Object
$User->create ();//Generate Data Objects
$User->add ();//Add new users Data
Or:
$User = D ("User"); Instantiate the User object
$User->create ()->add ();//Generate Data Objects and write data
The Create method, by default, generates data objects from the post data submitted by the form, and we can generate data objects from other data sources, and you can explicitly specify whether the currently created data object is automatically processed for new or edited data, for example:
$User = D ("User"); Instantiate
the user object $userData = getuserdata ()//(
$userData, 2) by means of the method $User->create, or the data object based on the UserData data. and specifies the update data
$User->add ();
The second parameter of the Create method is used to specify the completion time in the automatic completion rule, that is, the automatic processing rule for the Create method only handles automatic completion rules that meet the completion time. The Create method, when creating the data, non-data table field data information has been automatically filtered, so there is no need to worry that the form submits other illegal field information that results in a data object write error, or even automatically filters the field information that you do not want the user to submit in the form (see field legality filtering).
Ignore completes the rule, indicating that a field is omitted if it is left blank, and is usually used to modify the input of a user's data, as defined below:
1.5 Dynamic Completion
Cases:
$rules = Array (
' status ', ' 1 '), //When the Status field is set to 1 when new
Array (' Password ', ' MD5 ', 3, ' function '),//The password field is added and edited so that the MD5 function handles
array (' Update_time ', ' time ', 2, ') function '),//To write the current timestamp to the Update_time field at the time of the update
;
$User = M (' User ');
$User->auto ($rules)->create ()->add ();
1.6 Modifying Data Objects
After a data object is created using the Create method, the data object is saved in memory, so you can still manipulate the data object, including modifying or increasing the value of the data object, for example:
$User = D ("User"); Instantiate the User object
$User->create ();//Generate Data Object
$User->status = 2;//Modify the Status property of the data object
$User->register_ Time = Now_time; Add Register_time Property
$User->add ();//Add User Data
Example: Post forms submit data and then deal with some of the data in the form, such as time
$need = D ("need");
if ($need->create ()) {
$need->uid = $this->uid;
$need->time = Date (' Ymd '). '/need/'. $thumb _name;
}
$id = $need->add ();
if ($id) {
$this->success (' Add succeeded. ', U (' index ');
} else {
$this->error ($this->showregerror ($id));
}