ThinkPHP implements automatic verification and filling when using the M method (without creating model classes)

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




  1. $ Article = M ("Article ");
  2. // Automatic verification rules
  3. $ Validate = array (
  4. Array ('title', 'require ', 'title cannot be blank! ', 1 ),
  5. Array ('title', '', 'title already exists ', 0, 'Unique', 1 ),
  6. Array ('content', 'require ', 'content required', 1 ),
  7. );
  8. // Automatic filling rules
  9. $ _ Auto = array (
  10. // Set the status field to 1 when the restore operation is added.
  11. Array ('status', '1', self: MODEL_INSERT ),
  12. // When adding a timestamp, set the create_time field to the time () timestamp.

  13. Array ('create _ time', 'time', self: MODEL_INSERT, 'Function '),
  14. );
  15. $ Article-> setProperty ("_ validate", $ validate );
  16. $ Article-> setProperty ('_ auto', $ auto );

  17. // Create a data object
  18. If ($ Article-> create ()){
  19. // Data object created and written
  20. $ User-> add ();
  21. } Else {
  22. // An error occurred while creating the data object.
  23. Exit ($ Article-> getError ());
  24. }

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.

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.