abstrait:Model類 <?php namespace app\admin\model; use \think\Model; class UserModel extends Model { protected $table = 'user';
Model類 <?php namespace app\admin\model; use \think\Model; class UserModel extends Model { protected $table = 'user'; protected $pk = 'id'; } ?> Controller類 <?php namespace app\admin\controller; use app\admin\model\UserModel; use think\facade\Request; use app\admin\controller\Common; class User extends Common { public function index() { // 實(shí)例化模型 $user = new UserModel(); // 按id降序排列,并且每一頁(yè)設(shè)為八條數(shù)據(jù) $users = $user->order('id', 'desc')->paginate(8); // 將數(shù)據(jù)賦值給模板 $this->view->users = $users; // 渲染管理員界面 return $this->fetch(); } public function add() { // 渲染管理員添加界面 return $this->fetch(); } public function DoAdd() { // 獲取前臺(tái)提交過來(lái)的數(shù)據(jù) $data = Request::param(); // 獲取添加的時(shí)間 $data['time'] = time(); $username = $data['username']; // 使用用戶名來(lái)查詢數(shù)據(jù)庫(kù)是否有對(duì)應(yīng)的數(shù)據(jù) $res = UserModel::where('username', $username)->find(); // 判斷數(shù)據(jù)是否存在 if ($res == true) { return ['res' => 0, 'msg' => '用戶名已存在!']; } // 實(shí)例化模型 $user = new UserModel(); // 驗(yàn)證數(shù)據(jù)是否存入數(shù)據(jù)庫(kù) if ($user->save($data)) { return ['res' => 1, 'msg' => '添加成功!']; } else { return ['res' => 0, 'msg' => '添加失敗!']; } } public function edit() { // 獲取前臺(tái)提交過來(lái)的數(shù)據(jù) $userId = Request::param('id'); // 通過用戶id查詢需要更新用戶的所有數(shù)據(jù) $user = UserModel::get($userId); // 將數(shù)據(jù)賦值給模板 $this->view->user = $user; // 渲染編輯頁(yè)面 return $this->fetch(); } public function DoEdit() { // 獲取前臺(tái)提交過來(lái)的數(shù)據(jù) $data = Request::param(); // 實(shí)例化模型 $user = new UserModel(); // 對(duì)數(shù)據(jù)庫(kù)中的數(shù)據(jù)進(jìn)行修改更新 $res = $user->save([ 'username' => $data['username'], 'time' => time(), ], ['id' => $data['id']]); if ($res) { return ['res' => 1, 'msg' => '修改成功!']; } } public function del() { // 獲取需要?jiǎng)h除管理員的id $userId = Request::param('id'); // 實(shí)例化模型 $user = new UserModel(); // 進(jìn)行刪除并驗(yàn)證操作 if ($user->destroy($userId)) { // 返回提示信息 return['res'=>1,'msg'=>'刪除成功!']; } } }
Professeur correcteur:西門大官人Temps de correction:2019-04-10 10:25:02
Résumé du professeur:作業(yè)寫的很好,代碼結(jié)構(gòu)比較清晰,注釋合理。