The content of this article is about adding, deleting, modifying and checking PHP custom models. Now I share it with everyone. Friends in need can refer to it
<?php namespace app\index\controller; use app\common\model\User; use app\common\model\UserInfo; class Index { public function index() { /* // 静态访问 $user = User::get(1); // 获取一条记录的数据 dump($user->getData()); // 获取记录里面的某个属性值 dump($user->name); // 助手方法 dump(model('user')->find(1)->getData()); // 直接实例化 $user = new User(); dump($user->find(1)->getData()); */ $user = UserInfo::get(1); dump($user->getData()); $user = UserInfo::get(1); dump($user->getData()); return "thinkphp5"; } // 新增/更新 数据的方法 public function saveData() { // 新增一条数据 // $data = [ // 'name' => 'jikexueyuan', // 'password' => md5(123456), // 'age' => 18, // 'sex' => "男", // 'status' => 1, // 'create_time' => time(), // 'update_time' => 0, // ]; $user = new User(); // echo $user->save($data); // 新增多条数据 for($i = 0 ; $i < 10 ; $i ++) { $data[$i] = [ 'name' => 'jikexueyuan' . $i, 'password' => md5(123456), 'age' => $i, 'sex' => "男", 'status' => 1, 'create_time' => time(), 'update_time' => 0, ]; } $user->saveAll($data); } // 数据更新 public function updateData() { // 查询后进行更新操作 $user = User::get(16); $user->name = 'wangjialin'; echo $user->save(); // 直接使用数据库类的更新方法update $user = new User(); echo "return:".$user->where('id=18')->update(['name'=>'wjl']); // saveAll方法可以用于批量的更新 // [id=>1] } // 数据查询 public function getDataList() { // 无条件查询所有的数据 // $list = User::all(); // foreach ($list as $key=>$val) // { // dump("id:".$val->id.",name:".$val->name . ",age:".$val->age); // } // 条件列表查询 $list = User::all('18,22'); foreach ($list as $key=>$val) { dump("id:".$val->id.",name:".$val->name . ",age:".$val->age); } } // 数据删除 public function delData() { echo User::destroy(18); echo model('user')->where('id=16')->delete(); } }
Related recommendations:
PHP custom two-dimensional array sorting function array
Explanation on PHP custom functions and internal functions
The above is the detailed content of PHP custom model addition, deletion, modification and query. For more information, please follow other related articles on the PHP Chinese website!