ThinkPHP automatically verifies and fills ThinkPHP M methods when using the M method (without creating model classes ).
In general, we will write the automatic verification and automatic filling rules into the model class, and then use D
The method instantiates the model class to implement automatic verification and filling of forms. Because the M method only instantiates the basic model class and does not implement the logic defined in the model class, M
Generally, automatic verification and filling cannot be completed.
The setProperty method provided by ThinkPHP allows you to dynamically change the Model.
. In the operation, you can use the setProperty method to implement automatic verification and filling when using the M method (without creating a model class.
SetProperty
The method is a built-in method of the Model class, which is used to set the Model attribute value. The syntax is as follows:
SetProperty (string name, mixed
Value)
Example:
- $ Article = M ("Article ");
- // Automatic verification rules
- $ Validate = array (
- Array ('title', 'require ', 'title cannot be blank! ', 1 ),
- Array ('title', '', 'title already exists ', 0, 'Unique', 1 ),
- Array ('content', 'require ', 'content required', 1 ),
- );
- // Automatic filling rules
- $ _ Auto = array (
- // Set the status field to 1 when the restore operation is added.
- Array ('status', '1', self: MODEL_INSERT ),
- // When adding a timestamp, set the create_time field to the time () timestamp.
- Array ('create _ time', 'time', self: MODEL_INSERT, 'Function '),
- );
- $ Article-> setProperty ("_ validate", $ validate );
- $ Article-> setProperty ('_ auto', $ auto );
-
- // Create a data object
- If ($ Article-> create ()){
- // Data object created and written
- $ User-> add ();
- } Else {
- // An error occurred while creating the data object.
- Exit ($ Article-> getError ());
- }
To
The method uses automatic verification and automatic filling. you need to write data by using the data object method (create (). For more information, see ThinkPHP create
Method: smart writing of form data.
In principle, logic such as automatic verification and automatic filling should be completed in the model, but ThinkPHP provides setProperty
To enable automatic verification and filling in other special cases (such as when the M method is used.