Control Layer Indexcontroller
<?phpnamespace Home\controller; UseThink\controller;classIndexcontrollerextendsController { Public functionindex () {//$food = M (' food '); Two Tables union query//var_dump ($food->field (' a.food_id,b.user_id ')->table (' __food__ A, __person__ B ')->select ());
$food=d (' food '); Var_dump($food->sql_one ()->sql_two ()Select ()); $this-display (); } //The controller Create () method automatically invokes validation//If it is directly post, use the Create () method directly (pretend that the following is the post) Public functionLogin () {$person= D (' person '); $data[' name '] = ' 11 '; $data[' password '] = ' 1 '; $data[' repassword '] = ' 2 '; if($person->create ($data)) { Echo' All data verified successfully! ‘; } Else { Var_dump($person-GetError ()); } }}
Personmodel Layer
<?phpnamespace Home\model; UseThink\model;classPersonmodelextendsModel {//Batch Verification protected $patchValidate=true; //Automatically validate content protected $_validate=Array( Array(' name ', ' Require ', ' username must not be empty! ', 0, ' regex ', 3),//Array (' name ', ' email ', ' email ' format is incorrect! '),//array (' name ', ' url ', ' url ' format is not legal! '),//array (' name ', ' Currency ', ' currency format is incorrect! '),//array (' name ', ' Zip ', ' postal code must be a six-bit positive integer! '),//array (' name ', ' number ', ' must be a positive integer! '),//array (' name ', ' Integer ', ' must be an integer! '),//array (' name ', ' Double ', ' must be a floating-point number! '),//array (' name ', ' 中文版 ', ' must be in plain English! '),//array (' name ', '/^\d{3,6}$/', ' must be 3-6-bit pure number ', 0, ' regex '),//array (' name ', ' Li Tinghui ', ' pass over value not equal ', 0, ' equal ') ),//array (' name ', ' Li Tinghui ', ' passed over must be unequal ', 0, ' notequal '), Array(' Repassword ', ' password ', ' Two values not equal ', 0, ' confirm '),Array(' name ', ' checklength ', ' username must be in 3-5 bits ', 0, ' callback ', 3,Array(3,5)), ); //Custom Validation Methods protected functionChecklength ($str,$min,$max) { Preg_match_all("/./",$str,$matches); $len=Count($matches[0]); if($len<$min||$len>$max) { return false; } Else { return true; } } //encapsulating SQL statements in a model definition class protected $_scope=Array( ' Sql_one ' =Array( ' WHERE ' =Array(' food_id ' =Array(' GT ', 3)), ), ' sql_two ' =Array( ' Order ' =Array(' unit_price ' = ' DESC '), ' limit ' =>5, ), ); //function to create a new object PHP comes with a function Public function__construct () {Parent::__construct (); Var_dump(' \home '); } }
Eon thinkphp Handout thinkphp--automatic verification [Upper][12]