thinkphp automatic creation of data, auto-validation, auto-Completion Detailed example introduction (19)

Source: Internet
Author: User
Tags php class

Original: thinkphp auto-Create data, auto-validate, auto-Complete Detailed example introduction (19)


1: Automatically create data

//$name =$_post[' name '];
//$password =$_post[' password '); ---The post value that this registration page passes over
The following uses the auto-dress Create method can not use this, will act bound
$user =m (' user ');
$User->create (); Will pass the $_post data to the field of the database
$user->add (); You can write directly to


2: Automatic verification(you only need to define the $_validate attribute within the Model class)

Use Step (register to validate user table)


A: Create user table Custom mode--->home\lib\model\usermodel.class.php
<?php
//Note: Code username password .... is the name value of your form, which corresponds to the
//The field name within the form defines the
class Usermodel extends model{
protected $_validate=array (
Array (' Code ', ' Require ', ' verification code must be filled in! '),
Array (' Code ', ' checkcode', ' captcha error! ', 0, ' callback ', 1),//Use callback function Checkcode
Array (' username ', ' require ', ' user must fill in! '),
Array (' username ', ' ', ' user already exists ', 0, ' unique ', 1),
Array (' username ', '/^\w{6,}$/', ' username must be 6 letters above ', 0, ' regex ', 1),
Array (' Repassword ', ' Password ', ' Confirm password incorrect ', 0, ' confirm '),
);


protected function checkcode($code) {
if (MD5 ($code)!=$_session[' code ') {
return false;
}else{
return true;
}
}
}
?>


//Control please register the value corresponding method
<?php
class Registeraction extends action{
Public function Doreg () {

$user =d (' user '); The Big D method is to find your custom mode first (using the UserModel.class.php class created above)
//If there is a custom with custom, otherwise with the original D equivalent M m!=d
 
if (! $user->create ()) {//will automatically go to verify return a Boolean type, Success automatically creates data
$this->error ($user->geterror ());//Get specific error messages
}
$lastId = $user->add ();
if ($lastId) {
$this->redirect (' Index/index ');
}else{
$this->error (' User registration failed ');
}


}
}
?>

3: Auto-complete(example of making a message)
steps:
One: Create the message table Custom mode--->home\lib\model\messagemodel.class.php
<?php
class Messagemodel extends relationmodel{
protected $_auto=array (
Array (' Time ', ' time ', 1, ' function '),
Array (' uid ', ' getId', 1, ' callback ')

);
protected function getId() {
return $_session[' id '];
}
}
?>
II: In the Controller
<?php
class Messageaction extends action{
Public function Doliuyan () {
$message =d (' message ');
//$message->time=time ();
//$message->uid=$_session[' id ']; These 2 values will be automatically populated in the custom Messagemodel complete

$message->add ();//return value is the new ID number
}
}
?>







thinkphp automatic creation of data, auto-validation, auto-Completion Detailed example introduction (19)

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.