Thinkphp--create () method

Source: Internet
Author: User

The 1.create method can process the data that is submitted by the post (the data instance is automatically encapsulated by the field name in the table and the form's name, for example), such as a field in the user table called "username" if there is a <input name= "in the form" Username "value=" Xiao Ming ", then $user = M (' User '); $data = $User->create (); echo $data [' username '], will output "Xiao Ming", do not use $_post[' username ' to receive.
2. Use the Create method to make token validation of the form, preventing the form from repeating the submission.
3. Data can be automatically verified, provided that you have to manually create a UserModel.class.php file in the Model folder, in which to add validation
Protected $_validate = Array (
Array (' username ', ' require ', ' username must ', 1),
);
4. You can automatically assign a value to a field, if you have to manually create a UserModel.class.php file in the Model folder, in which you add
Protected $_auto = Array (
Array (' Create_time ', ' time ', Self::model_insert, ' function '),
);
Then the user's registration time will be automatically assigned to the current time

Attach the source code of the Create method:
/**
* Create data Objects but not save to database
* @access Public
* @param mixed $data Create data
* @param string $type status
* @return Mixed
*/
Public Function Create ($data = ', $type = ') {
If no value is passed by default for post data
if (empty ($data)) {
$data = $_post;
}elseif (Is_object ($data)) {
$data = Get_object_vars ($data);
}
Validating data
if (Empty ($data) | |!is_array ($DATA)) {
$this->error = L (' _data_type_invalid_ ');
return false;
}

Check field mappings
$data = $this->parsefieldsmap ($data, 0);

State
$type = $type? $type:(!empty ($data [$this-&GT;GETPK ()])? self::model_update:self::model_insert);

Check the legality of the Commit field
if (isset ($this->options[' field ')) {//$this->field (' field1,field2 ... ')->create ()
$fields = $this->options[' field '];
unset ($this->options[' field ');
}elseif ($type = = Self::model_insert && isset ($this->insertfields)) {
$fields = $this->insertfields;
}elseif ($type = = Self::model_update && isset ($this->updatefields)) {
$fields = $this->updatefields;
}
if (Isset ($fields)) {
if (is_string ($fields)) {
$fields = Explode (', ', $fields);
}
To determine the token validation field
if (c (' token_on ')) $fields [] = C (' Token_name ');
foreach ($data as $key = = $val) {
if (!in_array ($key, $fields)) {
Unset ($data [$key]);
}
}
}

Automatic data validation
if (! $this->autovalidation ($data, $type)) return false;

Form token validation
if (C (' token_on ') &&! $this->autochecktoken ($data)) {
$this->error = L (' _token_error_ ');
return false;
}

Validating the completion of the build data object
if ($this->autocheckfields) {//Turn on field detection to filter illegal field data
$fields = $this->getdbfields ();
foreach ($data as $key = = $val) {
if (!in_array ($key, $fields)) {
Unset ($data [$key]);
}elseif (MAGIC_QUOTES_GPC && is_string ($val)) {
$data [$key] = stripslashes ($val);
}
}
}

Automatic processing of data after creation
$this->autooperation ($data, $type);
Assigning the current data object
$this->data = $data;
Returns the data created for other calls
return $data;
}

The Add method is simple,
1. Insert the database into the data object created in the Create method.
For example:
$User = M (' User ');
$User->create (Array (' username ' = ' xiaoming ')); No data is received from the form here

$User->add ();
Insert into THINK_UESR (' username ') VALUES (' Xiao Ming ') will be generated
2. You can perform callback processing in the data, similar to the AOP idea of the spring framework in Java, if you have to manually create a UserModel.class.php file in the Model folder where you add
callback method before data is inserted
protected function _before_insert (& $data, $options) {

}
Callback method after a successful insert
protected function _after_insert ($data, $options) {

}
What to do before inserting, write your own logic in _before_insert, what to do after inserting, and write your own logic in _after_insert.

Thinkphp--create () method

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.