This article introduces the content of the PHP custom model additions and deletions, and now share to everyone, the need for friends can refer to
<?phpnamespace app\index\controller;use app\common\model\user;use app\common\model\userinfo;class Index{Public function index () {/*//static Access $user = User::get (1); Gets the data for a record dump ($user->getdata ()); Gets the value of a property in the record dump ($user->name); Helper method Dump (model (' user ')->find (1)->getdata ()); Direct instantiation of $user = new User (); Dump ($user->find (1)->getdata ()); */$user = Userinfo::get (1); Dump ($user->getdata ()); $user = Userinfo::get (1); Dump ($user->getdata ()); return "THINKPHP5"; }//How to add/Update data public Function SaveData () {//Add a new data//$data = [//' name ' = ' = ' Jike Xueyuan ',//' password ' + MD5 (123456),//' age ' + 18,//' sex ' = ' male ',// ' Status ' = 1,//' create_time ' + Time (),//' update_time ' + 0,//]; $user = New User ();//Echo $user->save ($data); Added multiple data for ($i = 0; $i < $i + +) {$data [$i] = [' name ' = ' = ' Jikexueyu An '. $i, ' Password ' and MD5 (123456), ' age ' and $i, ' sex ' and ' male ', ' Status ' = 1, ' Create_time ' + Time (), ' update_time ' = 0,]; } $user->saveall ($data); }//Data update public Function UpdateData () {///query after the update operation $user = User::get (16); $user->name = ' Wangjialin '; echo $user->save (); Update method for directly using the database class update $user = New User (); echo "return:". $user->where (' id=18 ')->update ([' name ' = ' WJL ']); The SaveAll method can be used for batch updates//[id=>1]}//Data query Public function getdatalist () {//unconditionally querying all data// $list = User::all ();//foreach ($list as $key = + $val)//{//Dump ("ID:". $val->id. ", Name:". $val->name. ", Age:". $val->age)//}//Condition list Query $list = User::all (' 18 , 22 '); foreach ($list as $key = = $val) {dump ("ID:". $val->id. ", Name:". $val->name. "," Age: ". $val->age) ; }}//Data delete public function Deldata () {echo User::d Estroy (18); echo model (' user ')->where (' id=16 ')->delete (); }}