亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

后臺管理員模塊增刪改查

原創(chuàng) 2019-04-09 13:15:08 269
摘要: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降序排列,并且每一頁設(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()
    {
        // 獲取前臺提交過來的數(shù)據(jù)
        $data = Request::param();
        // 獲取添加的時間
        $data['time'] = time();
        $username = $data['username'];
        // 使用用戶名來查詢數(shù)據(jù)庫是否有對應(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ù)庫
        if ($user->save($data)) {
            return ['res' => 1, 'msg' => '添加成功!'];
        } else {
            return ['res' => 0, 'msg' => '添加失敗!'];
        }

    }

    public function edit()
    {
        // 獲取前臺提交過來的數(shù)據(jù)
        $userId = Request::param('id');
        // 通過用戶id查詢需要更新用戶的所有數(shù)據(jù)
        $user = UserModel::get($userId);
        // 將數(shù)據(jù)賦值給模板
        $this->view->user = $user;

        // 渲染編輯頁面
        return $this->fetch();
    }

    public function DoEdit()
    {
        // 獲取前臺提交過來的數(shù)據(jù)
        $data = Request::param();
        // 實(shí)例化模型
        $user = new UserModel();
        // 對數(shù)據(jù)庫中的數(shù)據(jù)進(jìn)行修改更新
        $res = $user->save([
            'username' => $data['username'],
            'time' => time(),
        ], ['id' => $data['id']]);
        if ($res) {
            return ['res' => 1, 'msg' => '修改成功!'];
        }
    }

    public function del()
    {
        // 獲取需要刪除管理員的id
        $userId = Request::param('id');
        // 實(shí)例化模型
        $user = new UserModel();
        // 進(jìn)行刪除并驗(yàn)證操作
        if ($user->destroy($userId)) {
            // 返回提示信息
            return['res'=>1,'msg'=>'刪除成功!'];
        }
    }

}


批改老師:西門大官人批改時間:2019-04-10 10:25:02
老師總結(jié):作業(yè)寫的很好,代碼結(jié)構(gòu)比較清晰,注釋合理。

發(fā)佈手記

熱門詞條