Instance analysis of the create method for smart writing of ThinkPHP form data

Source: Internet
Author: User
This article mainly introduces the create method for smart writing of ThinkPHP form data, and analyzes in detail the related techniques that can only be written to create in ThinkPHP in the form of instances, which has some reference value, for more information about how to intelligently write ThinkPHP form data to the create method, see the example in this article. Share it with you for your reference. The details are as follows:

Create ()

In addition to manually constructing a data set to be written into the database, ThinkPHP also provides the create () method for automatic creation of data objects. The create () method automatically collects submitted form data and creates data objects without manual intervention, which is more advantageous when there are many form data fields.

Create () is used to write the preceding data into the form:

Public function insert2 () {header ("Content-Type: text/html; charset = utf-8"); $ Dao = M ("User "); if ($ Dao-> create () {$ Dao-> password = md5 ($ _ POST ["password"]); $ Dao-> regdate = time (); if ($ lastInsId = $ Dao-> add () {echo "insert data id: $ lastInsId" ;}else {echo "data writing error! ";}} Else {exit ($ Dao-> getError (). '[return]');}

After creating a data object, create () automatically collects the submitted form data. The form data may need to be processed (for example, encrypted) before being written to the data table. Therefore, you can modify or add or remove the member attribute values of the data object.

Tip: the data objects created by create () are stored in the memory and can be modified before the add () or save () operation is executed.

In the preceding example, the action of the create () method is the same as that of the date () method. However, the date () method is only simple to create data objects, but the create () method also has the following:

① Token verification
② Automatic data verification
③ Field ing support
④ Field type check
⑤ Automatic completion of data

To complete these advanced data model functions, you must use the D method to instantiate the data model. ThinkPHP provides various verification and filling rules for calling. for details, see ThinkPHP automatic verification and ThinkPHP automatic filling.

Automatic verification and filling

Before writing a form into a data table, there are often some data checks (whether the submitted user name meets the requirements) and processing (such as password encryption in the example and obtaining the current timestamp ). The create () method supports automatic data verification and completion.

Create the UserModel. class. php file in the LibModel Directory (the User is the created model object, which also corresponds to the prefix _ user table), and add automatic verification and automatic filling rules:

Class UserModel extends Model {// automatic verification setting protected $ _ validate = array ('username', 'require ', 'username is required! ', 1), array ('email', 'Email ', 'Email format error! ', 2), array ('username', '',' the user name already exists! ', 0, 'Unique', 1),); // Set protected $ _ auto = array ('regdate', 'time', self: MODEL_INSERT, 'function'), array ('password', 'md5', self: MODEL_INSERT, 'Function '),);}

Change the insert2 operation:

Public function insert2 () {header ("Content-Type: text/html; charset = utf-8"); $ Dao = D ("User "); if ($ Dao-> create () {if ($ lastInsId = $ Dao-> add () {echo "insert data id: $ lastInsId ";} else {echo "data writing error! ";}} Else {exit ($ Dao-> getError (). '[return]');}

If the submitted data does not meet the verification requirements (such as the user name), create () fails to create the data object (FALSE is returned), $ Dao-> getError () A prompt message is displayed, indicating that the user name already exists!

After the verification rule is passed, the system automatically fills in the settings, encrypts the form password with MD5, and obtains the current timestamp to fill in the data object of create.

Therefore, the combination of the D method and the create () method is very intelligent and powerful, and the proper use can achieve the goal of quick development with half the effort.

Tip:

① D the method works with create () because of its powerful functions, it also loses some efficiency. we recommend the M method + data () mode when the business logic is not complex.

② Create () Accepts POST data by default. to accept other types of data, you only need to specify the parameter, for example, accept GET data: create ($ _ GET)

I hope this article will help you design php programs based on the ThinkPHP framework.

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.