thinkphp form Data Intelligent Write Create method instance analysis _php instance

Source: Internet
Author: User
Tags md5 php file php programming autofill settings

This example describes the thinkphp form data Intelligent Write create method. Share to everyone for your reference. Specifically as follows:

Creating a Data Object Create ()

In addition to manually constructing a data set for warehousing, thinkphp also provides a create () method for automatically creating data objects. The Create () method will automatically collect submitted form data and create data objects without human intervention, which is more advantageous in the case of very many form data fields.

The example that writes the preceding text 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: $lastInsId ";
  } else {
   echo" Data write Error! ";
  }
 } else{
  exit ($Dao->geterror (). ' [<a href= ' Javascript:history.back () ' > Return </a>] ');
 }


When Create () creates a data object, the submitted form data is automatically collected. The form data may need to be processed (for example, password encryption) to write to the datasheet, so you can modify or add to the member property values of the data object.

Tip: Create () Creates a data object that is stored in memory and can be modified before the inbound action (add () or save ()) is executed.

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

① Token Verification
② Data Automatic Verification
③ Field Mapping support
④ field type check
⑤ data is automatically completed

To complete these advanced data model functions, you need to use the D method to instantiate the data model. Thinkphp provides a variety of validation and fill rules for the call, specifically see "thinkphp Automatic Verification" and "thinkphp automatic filling" related articles.

Automatic validation and automatic padding

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

Create the UserModel.class.php file in the Libmodel directory (User is the model object created, also the prefix _user table), add automatic validation and AutoFill rules:

Class Usermodel extends model{
 //automatic authentication settings
 protected $_validate = Array (
  ' username ', ' require ', ' User name must be filled in! ', 1,
  array (' email ', ' email ', ' mailbox format error!) ', 2,
  Array (' username ', ', ', ' username already exists! ', 0, ' unique ', 1,
 );
 AutoFill settings
 protected $_auto = 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: $lastInsId;
  } else {
   echo Data write Error! ";
  }
 } else{
  exit ($Dao->geterror (). ' [<a href= ' Javascript:history.back () ' > Return </a>] ');
 }


If the submitted data does not meet the validation requirements (such as the existence of a user name), create () creates a data object that fails (returns FALSE), $Dao->geterror () prints the prompt that is set in the Automatic Validation settings rule: Username already exists!

If the validation rule is passed, the system will automatically populate the settings, MD5 the form password, and get the current timestamp to populate the Create () data object.

So the D method with Create () is very intelligent and powerful, appropriate use can achieve the goal of faster development with less effort.

Tips:

①d method with Create () because of its powerful, also lost a certain degree of efficiency, in the business logic is not complex to recommend M method +data () mode

②create () accepts POST data by default, and to accept other types of data, simply specify within parameters, such as accept get data: Create ($_get)

More interested in thinkphp related content readers can view the site topics: "thinkphp Introductory Course" and "thinkphp Common methods Summary"

I hope this article will help you with your PHP programming 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.