Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to intelligently write ThinkPHP form data into create method

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to intelligently write ThinkPHP form data into create. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

The details are as follows:

Create a data object create ()

In addition to manually constructing the stored dataset, ThinkPHP provides a create () method to automatically create data objects. The create () method automatically collects submitted form data and creates data objects without human intervention, which is even more advantageous in cases where there are a large number of form data fields.

The example of writing the previous article to the form data is implemented with create ():

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 is: $lastInsId";} else {echo "data write error!" ;} else {exit ($Dao- > getError (). [return]');}}

After create () creates the data object, it automatically collects the submitted form data. On the other hand, the form data may need to be processed (such as encrypting the password) before it can be written into the data table, so the member attribute values of the data object can be modified or added and removed according to it.

Tip: the data objects created by create () are stored in memory and can be modified before performing the storage action (add () or save ()).

In the above example, the behavior of the create () method is consistent with that of the date () method. But the date () method simply creates a data object, but the create () method also has:

① token authentication

Automatic verification of ② data

③ field mapping support

④ field type check

Automatic completion of ⑤ data

In order to complete these advanced data model functions, we need to use D method to instantiate the data model. ThinkPHP provides a variety of validation and padding rules to call, as described in the articles "ThinkPHP Auto-validation" and "ThinkPHP Auto-populate".

Automatic verification and automatic filling

Before writing the form to the data table, there is often some detection of the data (whether the submitted user name meets the requirements) and processing (such as password encryption in the example and getting the current timestamp). The create () method supports automatic validation and completion of data.

Create a UserModel.class.php file under the LibModel directory (User is the model object created and also corresponds to the prefix _ user table), and add automatic verification and auto-filling rules:

Class UserModel extends Model {/ / automatic verification setting protected $_ validate = array (array ('username','require',' user name must be filled in!' , 1), array ('email','email',' mailbox format error!' , 2), array ('username','',' user name already exists!' / / automatic filling setting protected $_ auto = array (array ('regdate','time',self::MODEL_INSERT,'function'), array (' password','md5',self::MODEL_INSERT,'function'),);}

Change the insert2 operation to:

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 is: $lastInsId";} else {echo "data write error!" ;} else {exit ($Dao- > getError (). [return]');}}

If the submitted data does not meet the verification requirements (such as the user name exists), create () fails to create the data object (returning FALSE), and $Dao- > getError () will print out the prompt set in the automatic verification setting rule: the user name already exists!

If the validation rule is passed, the system automatically populates, encrypts the form password with MD5, and gets the current timestamp to populate the data object of create ().

So D method with create () is very intelligent and powerful, proper use can achieve the goal of rapid development with twice the result with half the effort.

Tip:

Because of its powerful function, ① D method and create () lose some efficiency. M method + data () mode is recommended when the business logic is not complex.

② create () accepts POST data by default. To accept other types of data, you only need to specify it in the parameter, such as GET data: create ($_ GET)

On "ThinkPHP form data how to intelligently write create method" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report