Home>

Create data object create ()

In addition to manually constructing the inbound data set,thinkphp also provides a create () method that automatically creates data objects. The create () method will automatically collect submitted form data and create data objects without human intervention,This is an advantage when there are many form data fields.

The example of writing the preceding data into the form is implemented using 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 writing error!";
  }
 } else {
  exit ($dao->geterror (). "[<a href =" javascript:history.back () ">return</a>]");
 }
}

After create () creates the data object,Submitted form data will be collected automatically.Form data may require some processing (such as password encryption) to be written into the data table.So you can modify or add or remove member attribute values ​​of the data object.

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

In the example above,The create () method behaves the same as the date () method. But the date () method simply creates a data object,But the create () method also has:

① Token verification

② Automatic data verification

③ Field mapping support

④ Field type check

⑤ Data is completed automatically

And other advanced data functions,To complete these advanced data model functions,Need to use the d method to instantiate the data model.thinkphp provides various validation and population rules for invocation,For details, please refer to "thinkphp automatic verification" and "thinkphp autofill" related articles.

Automatic verification and autofill

Before writing the form to a data table,Often there will be 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 completion of data.

Create the usermodel.class.php file in the libmodel directory (user is the model object created,(Also corresponds to the prefix _user table), adding auto-authentication and auto-population rules:

class usermodel extends model {
 //Automatic verification settings
 protected $_validate=array (
  array ("username", "require", "User name must be filled in!", 1),  array ("email", "email", "Wrong mailbox format!", 2),  array ("username", "", "The username already exists!", 0, "unique", 1), );
 //Autofill settings
 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:$lastinsid";
  } else {
   echo "Data writing error!";
  }
 } else {
  exit ($dao->geterror (). "[<a href =" javascript:history.back () ">return</a>]");
 }
}

If the submitted data does not meet the verification requirements (such as the user name exists), create () fails to create the data object (returns false), $dao->geterror () will print out the prompt information set in the automatic verification setting rule:user The name already exists!

If the validation rules pass,The system will set up autofill.Encrypt the form password with md5 and get the current timestamp to fill the create () data object.

So the method d and create () are very smart and powerful.Proper application can achieve the goal of rapid development with less effort.

prompt:

① The d method works with create ()It loses some efficiency,M method + data () mode is recommended when the business logic is not complicated

② create () accepts post data by default. To accept other types of data,Just specify it in the parameters,If accepting get data:create ($_ get)

php
  • Previous The most complete collection of commonly used regular expressions-including check digits, characters, some special needs, etc
  • Next JavaMe development adaptive scroll display