模型增删改
//新增 public function add() { $user = new user(); $user->uid = 7; $user->uname = '六元'; $user->email = '111qq.com'; $user->birthday = strtotime('1989-7-11'); if ($user->save()) { User::get(1); echo User::getLastSql(); } //另一种方式---------------------- $user['uid'] = 6; $user['uname'] = '六元'; $user['email'] = '111qq.com'; $user['birthday'] = strtotime('1989-7-11'); if ($res = user::create($user)) { return "新增成功"; } } //批量新增 public function addlist() { $user = new User; $list = [ ['uname' => '张三', 'email' => 'zsqq@qq.com'], ['uname' => '里斯', 'email' => 'onethink@qq.com'] ]; $user->saveAll($list); echo User::getLastSql(); $user = new user; $list = [ ['uid' => 7, 'uname' => '张三', 'email' => 'zsqq.com', 'birthday' => strtotime('1978-8-7')], ['uid' => 8, 'uname' => '里斯', 'email' => 'lsqq.com', 'birthday' => strtotime('1978-8-7')], ]; dump($user->saveAll($list)); if ($user->saveAll($list)) { return "数据添加成功"; } } //查找更新数据 public function update() { $user = user::get(5); $user->uname = "赛文"; if ($user->save()) { return '修改成功'; // } dump($user); //更新数据 $user = user::get(5); $user->save(['uname' => '迪迦']); // 批量更新数据 $user = new user(); $list = [ ['uid' => 5, 'uname' => "赛文"], ['uid' => 6, 'uname' => '迪迦'], ]; if ($user->saveAll($list)) { return "批量修改成功"; // } // //实例化加数据库类方法实现 $user = new user(); $user->update(['uid' => 5, 'uname' => '易建联']); $user::update(['uid' => 5, 'uname' => '陈浩南']); } //模型查询数据 public function selects() { $user = user::get(1); echo $user->uname; // //第二种方法 $user = user::get(['uname' => '迪迦']); echo $user->uid; //获取多个数据 $list = user::all([1, 2, 3]); foreach ($list as $key => $value) { echo $value->uname; // } //取出方法二 $user = new user(); $res = $user->where('uid', 1)->select(); foreach ($res as $key => $value) { echo $value['uname']; } dump($res); } //删除数据 public function deldata() { $user = user::get(1); if ($user->delete()) { return "删除成功"; // } //删除单条数据 if (user::destroy(2)) { return "删除成功"; } //删除多条数据 if (user::destroy([5, 6, 7])) { return "删除多条数据成功"; } }