yii 用户管理 controller

namespace app\modules\admin\controllers;
use yii\web\Controller;

class BaseController extends Controller{
    
    
    public function url(){
        return \Yii::$app->urlManager;
    }

}


 
  
request= \Yii::$app->request;
    }

    /**
     * 用户列表
     */
    public function actionList(){
        
         $userModel=new User();
         $query=$userModel->find()->where(array());
         $countQuery=  clone $query;
         $pages=new Pagination(array('totalCount'=>$countQuery->count()));
         $pages->defaultPageSize=5;
         $result=$query->offset($pages->offset)->limit($pages->limit)->all();
         
         $roleModel=new Role();
         $role=$roleModel->find()->all();
            
         return $this->renderPartial("list",array('model'=>$result,'pages'=>$pages,"roles"=>$role));
    }
    
    /**
     * 添加用户
     */
    public function actionAdd(){
        if( $this->request->getIsPost()){
            $post=$this->request->post();
            unset($post['_csrf']);
            $userModel=new User();
            if($userModel->checkRepeat($post)){
                exit("不能添加重复记录");
            }
            $userModel->scenario="add"; 
            $userModel->attributes=$post;
            if( $userModel->insert() ){
                return $this->redirect($this->url()->createUrl("admin/user/list"));
            }     
            return $this->redirect($this->url()->createUrl("admin/user/add"));
        }else{
            $roleModel=new Role();
            $role=$roleModel->find()->all();
            return $this->renderPartial("add",array("roles"=>$role));
        }
    }
    
    /**
     * 更新用户
     */
    public function actionUpdate(){
        $userModel=new User();
        if( $this->request->getIsPost()){
            $post=$this->request->post();
            if($post['password']==""){
                unset($post['password']);
            }
            unset($post['_csrf']);
            $connection= \Yii::$app->db;
            //生成键值对
            $str="";
            $uid=$post['user_id'];
            unset($post['user_id']);
            if(!empty($post)){
                foreach ($post as $k=>$v){
                    if($v!=""){
                        $str.="`".$k."`='".$v."',";
                    }
                }
                $str=  rtrim($str,",");
                $sql="UPDATE `tb_user` SET  $str  WHERE `user_id`=:uid";
                $update=$connection->createCommand($sql);
                $result=$update->bindValue(':uid',$uid)->execute();
                if( $result ){
                    return $this->redirect($this->url()->createUrl("admin/user/list"));
                }
            }
           
            return $this->redirect($this->url()->createUrl(array("admin/user/update",'uid'=>$post['user_id'])));
        }else{
            $roleModel=new Role();
            $role=$roleModel->find()->all();
            $uid=  $this->request->get("uid");
            $result=$userModel->findOne($uid);
            return $this->renderPartial("update",array('model'=>$result,"roles"=>$role));
        }
    }
    
    /**
     * 删除用户
     */
    public function actionDelete(){
        $uid=  $this->request->get("uid");
        if(empty($uid)){
            return false;
        }
        $userModel=new User();
        $newModel=$userModel->findOne($uid);
        $newModel->scenario='delete';
        if($newModel->delete()){
             return $this->redirect($this->url()->createUrl("admin/user/list"));
        }
         return "删除失败";
    }
    
    /**
     * 查看详情
     */
    public function actionDetial(){
        $uid=  $this->request->get("uid");
        if(empty($uid)){
            return false;
        }
       
        $userModel=new User();
        $newModel=$userModel->findOne($uid);
        $roleModel=new Role();
        $role=$roleModel->find()->where("role_id=".$newModel->role_id)->one();
        $newModel->role_id=$role->role_name;
        return $this->renderPartial("detial", array("model"=>$newModel));
    }
    
}

你可能感兴趣的:(yii 用户管理 controller)