Model Layer
<?php namespace App\demo\model; UseThink\model; classStudentextendsmodel{//The capture device Public functionGetssexattr ($val){ Switch($val){ Case1:returnMale; Break; Case2:returnFemale; Break; default:returnUnknown; Break; } } //modifier Public functionSetclassattr ($val){ return MD5($val); } //setting, if there is no assignment, the fields that need to be completed automatically Public $auto= [' Sno ', ' Ssex ']; Public functionsetsnoattr () {return18; } Public functionsetssexattr () {return1111; } }?>
Control layer
<?php namespace App\demo\controller; UseThink\controller; UseApp\demo\model\student;//Introducing Models classDemoextendscontroller{ Public functionTest () {//get the data for ID 4 in the student table $student=NewStudent; //Dump ($student->get (4)->toarray ()); Query operation//1 Condition query//dump ($student->get (["Sno" =>101])->toarray ()); 2 Closed packet Query /*$data = $student->get (function ($query) {$query->where (' Sname ', ' Sun Quan '); }); Dump ($data->toarray ());*/ //3 Find Method//$data = $student->where (' Sname ', ' Liu Bei ')->find (); 4 querying more than one piece of data /*$data = Student::all (' All-in-all '); foreach ($data as $key = = $stu) {dump ($stu->toarray ()); }*/ //5 inserting data//inserting a bar /*$student->data ([' id ' = ' = ', ' Sno ' = ' + ', ' Sname ' = ' haha ', ' Ssex ' and ' = ' male ', ' sbirthday ' and ' = ', ' Class "=" 96080 "]); $student->save (); Insert multiple $list = [[' id ' = ' = ', ' Sno ' = ' + ', ' Sname ' = ' haha ', ' Ssex ' and ' man ', ' sbirthday ' and ' = ' man ', ' Class "+" 96080 "], [' id ' = = ', ' Sno ' = ' + ', ' Sname ' and ' haha ', ' Ssex ' and ' man ', ' sbirthday ' and ' man ', ' Class ' = > "96080"]; Dump ($user->saveall ($list, false)); */ //Update /*$student->save ([' Sname ' = ' Zhang Liao ', ' Sno ' + 201], [' id ' = 1]);*/ //Delete//student According to primary key::d Estroy ([+]); Conditional Delete: Delete data with id 0//student::d estroy ([' id ' = 0]); The capture device /*$data = $student->get ([' id ' = ' 7 ']); Dump ($data->toarray ());*/ //modifier /*$data = $student->save ([' Class ' = ' 345 '],[' id ' = ' 9 ']); Dump ($data);*/ //Auto-Finish//There is no value for age and sex here /*$data = $student->save ([' Class ' = ' 345 '],[' id ' = ' 8 ']); Dump ($data);*/ } } //time Stamp /*the system supports automatically writing timestamp fields created and updated the first way is to add global settings to the database configuration file://Turn on the auto-write timestamp field ' Auto_timestamp ' + true, and the second is to set directly in a separate model class Reset: Protected $autoWriteTimestamp = true; If these two places are set to True, the default is recognized as int type, and if your time field is not of type int, for example, using the datetime type, you can set://Turn on the auto-write timestamp field ' auto_timestamp ' = ' datet IME ', or protected $autoWriteTimestamp = ' datetime ';*/?>
8.5 Database model Operations