ThinkPHP form data smart write create Method Instance analysis _ php instance

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.


① 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)

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: 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.