项目准备(model)

user model manage

<?php
    /**
     * UserModelManager is a class used to manage UserModel class
     * @author liupengcheng
     * @date 2014-3-3
     */
    class UserModelManager{
        private $dao;
        private $_si;
        private $db_name;
        private $table_name;
        private $table_apply;

        public function __construct(){
                $this->dao = new DAO();
                $this->_si = new SqlIntepreter();
                $this->db_name=Loader::loadConfigs(‘db_usermodel‘,‘db1‘);
                $this->table_name=Loader::loadConfigs(‘db_usermodel‘,‘table1‘);
                $this->table_apply=Loader::loadConfigs(‘db_usermodel‘,‘table2‘);
        }

        public function userExist($user_name, $password){
            $select =array();
            array_push($select,‘user_name‘);
            array_push($select,‘password‘);

            try{
                $this->dao->connect();  //连接数据库
                $this->dao->selectDB($this->db_name); //选择数据库
            }catch(Exception $e){
                throw $e;
            }
            $where = array(‘user_name‘=>$user_name,‘password‘=>$password);
            //重置状态
            $this->_si->renew();
            $sqlstat = $this->_si->select($select)->from($this->table_name)->where($where)->getSql();   //构造sql语句
            $result=$this->dao->execute($sqlstat);
            $n = mysql_num_rows($result);
            mysql_close();
            if ($n>0) {
                return true;
            }
            return false;

        }
        /**
         *
         * @param $user_name
         * @param $password
         * @param string $limit must be integer
         * @throws Exception
         * @return resource
         */
        public function getUsers($user_name,$password,$limit=null){
            $select=array();
            array_push($select,‘user_name‘);
            array_push($select, ‘password‘);
            array_push($select, ‘role‘);
            array_push($select,‘email‘);
            array_push($select,‘comment‘);
            try{
                $this->dao->connect();
                $this->dao->selectDB($this->db_name);
            }catch(Exception $e){
                throw $e;
            }
            $where = array(‘user_name‘=>$user_name,‘password‘=>$password);
            //重置状态
            $this->_si->renew();
            if(!isset($limit)){
                $sqlstat = $this->_si->select($select)->from($this->table_name)->where($where)->getSql();
            }else{
                $sqlstat = $this->_si->select($select)->from($this->table_name)->where($where)->limit($limit)->getSql();
            }
            $result = $this->dao->execute($sqlstat);
            $users = mysql_fetch_array($result);
            mysql_close();
            return new UserModel($users);
        }

        public function addUser($user_name,$password,$role,$email,$comment){
            $insert=array(‘user_name‘,‘password‘,‘role‘,‘email‘,‘comment‘);
            $values = array(‘user_name‘=>$user_name,‘password‘=>$password,‘role‘=>$role,‘email‘=>$email,‘comment‘=>$comment);
            //重置状态

            $this->_si->renew();
            $sqlstat = $this->_si->insert($this->table_name, $insert)->values($values)->getSql();

            try{
                $this->dao->connect();
                $this->dao->selectDB($this->db_name);
            }catch(Exception $e){
                throw $e;
            }
            $result=$this->dao->execute($sqlstat);
            if(mysql_affected_rows()>0){
                mysql_close();
                return true;
            }
            return false;
        }

        public function delUser($id){
            $where=array(‘id‘=>$id);
            //重置状态
            $this->_si->renew();
            $sqlstat = $this->_si->delete($this->table_name)->where($where);
            try{
                $this->dao->connect();
                $this->dao->selectDB($this->db_name);
            }catch(Exception $e){
                throw $e;
            }
            $result = $this->dao->execute($sqlstat);
            if(mysql_affected_rows()>0){
                mysql_close();
                return true;
            }
            else
                return false;
        }

        public function userNameAvailable($user_name){
            try{
                $this->dao->connect();
                $this->dao->selectDB($this->db_name);
            }catch(Exception $e){
                throw $e;
            }

            $user_table = true;
            $apply_table = true;
            $where = array(‘user_name‘=>$user_name);

            //重置状态
            $this->_si->renew();
            $sqlstat = $this->_si->select()->from($this->table_name)->where($where)->getSql();
            $result = $this->dao->execute($sqlstat);

            if(mysql_num_rows($result)>0){
                $user_table = false;
            }

            $this->_si->renew();
            $sqlstat = $this->_si->select()->from($this->table_apply)->where($where)->getSql();
            $result = $this->dao->execute($sqlstat);

            if(mysql_num_rows($result)>0){
                $apply_table = false;
            }

            mysql_close();

            if($user_table && $apply_table){
                return true;
            }else{
                return false;
            }

        }

        public function updateBasicInfo($role=‘‘, $email=‘‘, $comment=‘‘){
            session_start();
            $sqlstat=null;
            if(isset($_SESSION[‘user_name‘])&&!empty($_SESSION[‘user_name‘])){
                $where=array(‘user_name‘=>$_SESSION[‘user_name‘]);
                $params=array();
                $role = empty($role)? $_SESSION[‘role‘]:$role;
                $email = empty($email) ? $_SESSION[‘email‘]:$email;
                $comment = empty($comment)? $_SESSION[‘comment‘]:$comment;
                $params=array(‘role‘=>$role,‘email‘=>$email,‘comment‘=>$comment);
                //重置状态
                $this->_si->renew();
                $sqlstat=$this->_si->update($this->table_name)->set($params)->where($where)->getSql();
            }else{
                throw new Exception("Error: session expired");
            }
            try{
                $this->dao->connect();
                $this->dao->selectDB($this->db_name);
            }catch(Exception $e){
                throw $e;
            }
            $result=$this->dao->execute($sqlstat);
            if(mysql_affected_rows()>0){
                mysql_close();
                return true;
            }else
                return false;
        }

        public function passUpdate($pass, $newPass){
            session_start();
            $sqlstat=null;
            if(isset($_SESSION[‘user_name‘])&&!empty($_SESSION[‘user_name‘])){
                $where=array(‘user_name‘=>$_SESSION[‘user_name‘],‘password‘=>$pass);
                $set=array(‘password‘=>$newPass);
                //重置状态
                $this->_si->renew();
                $sqlstat=$this->_si->update($this->table_name)->set($set)->where($where);
            }else{
                throw new Exception(‘Error: session expired‘);
            }
            try{
                $this->dao->connect();
                $this->dao->selectDB($this->db_name);
            }catch(Exception $e){
                throw $e;
            }
            $result = $this->dao->execute($sqlstat->getSql());
            if(mysql_affected_rows()>0){
                mysql_close();
                return true;
            }else
                return false;
        }

        public function getapplycount(){   //获得apply表的个数-hss
            try{
                $this->dao->connect();
                $this->dao->selectDB($this->db_name);
            }catch(Exception $e){
                throw $e;
            }

            $this->_si->renew();

            $sqlstat = $this->_si->select()->from($this->table_apply)->getSql();   //构造sql语句
            //return $sqlstat;
            $result=$this->dao->execute($sqlstat);   //数据库执行的问题

            $n = mysql_num_rows($result);
            mysql_close();

            return $n;
        }

        public function getApplyUserInfo(){
            try{
                $this->dao->connect();
                $this->dao->selectDB($this->db_name);
            }catch(Exception $e){
                throw $e;
            }

//             //重置状态
            $this->_si->renew();
            if(!isset($limit)){
                $sqlstat = $this->_si->select()->from($this->table_apply)->getSql();
            }else{
                $sqlstat = $this->_si->select()->from($this->table_apply)->limit($limit)->getSql();
            }

            $result = $this->dao->execute($sqlstat);

            $users=array();

            $user=array();
            while($row = mysql_fetch_array($result)){
                $user[‘name‘]=$row["user_name"];
                $user[‘role‘]=$row["role"];
                if(isset($row["email"])){
                    $user[‘email‘]=$row["email"];
                }else{
                    $user[‘email‘]="";
                }
                if(isset($row["comment"])){
                    $user[‘comment‘]=$row["comment"];
                }else{
                    $user[‘comment‘]="";
                }
                array_push($users, $user);
            }

            mysql_close();
            return json_encode($users);
        }

        //把申请用户从apply_info中转到user_info中,删除apply_info中的这些用户
        public function addApplyUser($names){
            try{
                $this->dao->connect();
                $this->dao->selectDB($this->db_name);
            }catch(Exception $e){
                throw $e;
            }

            $add_user_info = array();
            $opera_status = true;
            //从apply_info中读取申请用户,并删除这些申请用户
            foreach ($names as $name){
                $this->_si->renew();
                $where = array(‘user_name‘=>$name);

                //读用户
                $sqlstat = $this->_si->select()->from($this->table_apply)->where($where)->getSql();
                $read_result = $this->dao->execute($sqlstat);

                $user= mysql_fetch_array($read_result);
                $read_state = false;
                if(mysql_affected_rows()>0){
                    $read_state = true;
                }
                array_push($add_user_info, $user);

                //删除用户
                $this->_si->renew();
                $sqlstat = $this->_si->delete($this->table_apply)->where($where)->getSql();
                $delete_result = $this->dao->execute($sqlstat);
                $delete_state = false;
                if(mysql_affected_rows()>0){
                    $delete_state = true;
                }

                if(!($read_state && $delete_state)){
                    $opera_status = false;
                    break;
                }
            }

            //添加apply_info中读取的用户到users_info中
            foreach ($add_user_info as $user){
                $insert=array(‘user_name‘,‘password‘,‘role‘,‘email‘,‘comment‘);
                $values = array(‘user_name‘=>$user[‘user_name‘],
                                    ‘password‘=>$user[‘password‘],
                                    ‘role‘=>$user[‘role‘],
                                    ‘email‘=>$user[‘email‘],
                                    ‘comment‘=>$user[‘comment‘]);

                $this->_si->renew();
                $sqlstat = $this->_si->insert($this->table_name, $insert)->values($values)->getSql();
                $result=$this->dao->execute($sqlstat);
                if(!(mysql_affected_rows()>0)){
                    $opera_status = false;
                    break;
                }
            }
            mysql_close();
            //重置状态
            return $opera_status;
        }

        //把申请用户从apply_info中删除
        public function  deleteApplyUser($names){
            try{
                $this->dao->connect();
                $this->dao->selectDB($this->db_name);
            }catch(Exception $e){
                throw $e;
            }

            $add_user_info = array();
            $delete_state = true;
            //从apply_info中读取申请用户,并删除这些申请用户
            foreach ($names as $name){
                $this->_si->renew();
                $where = array(‘user_name‘=>$name);

                //删除用户
                $this->_si->renew();
                $sqlstat = $this->_si->delete($this->table_apply)->where($where)->getSql();
                $delete_result = $this->dao->execute($sqlstat);

                if(!(mysql_affected_rows()>0)){
                    $delete_state = false;
                    break;
                }

            }
            mysql_close();
            //重置状态
            return $delete_state;
        }
    }
时间: 2024-10-15 22:57:57

项目准备(model)的相关文章

解决maven项目将model version改成3.0版本问题

找到项目目录,找到.setting文件 找到org.eclipse.wst.common.project.facet.core.xml文件 修改如下标签 <installed facet="jst.web" version="3.0"/> 重启Eclipse即可生效

一个ActionResult中定位到两个视图&mdash;&lt;团委项目&gt;

     在使用MVC做项目的时候一般的情况就是一个ActionResult一个视图,这样对应的Return View();就可以找到下面对应的视图,这是根据一个原则,"约定大于配置",但是我们有的时候需要在一个ActionResult中根据业务跳转到不同的视图,展示到界面上. 这里也不一定绝对要跳转到对于的视图,我们可以通过Return RedirectToAction()来跳转到对于的视图,如下 public ActionResult Detail(Guid id) { try {

MVC项目实践,在三层架构下实现SportsStore-01

SportsStore是<精通ASP.NET MVC3框架(第三版)>中演示的MVC项目,在该项目中涵盖了MVC的众多方面,包括:使用DI容器.URL优化.导航.分页.购物车.订单.产品管理.图像上传......是不错的MVC实践项目,但该项目不是放在多层框架下开发的,离真实项目还有一段距离.本系列将尝试在多层框架下实现SportsStore项目,并用自己的方式实现一些功能. 本篇为系列第一篇,包括: ■ 1.搭建项目■ 2.卸载Entity Framework组件,并安装最新版本■ 3.使用

MVC项目实践,在三层架构下实现SportsStore-01,EF Code First建模、DAL层等

http://www.cnblogs.com/darrenji/p/3809219.html 本篇为系列第一篇,包括: ■ 1.搭建项目■ 2.卸载Entity Framework组件,并安装最新版本■ 3.使用EF Code First创建领域模型和EF上下文■ 4.三层架构设计    □ 4.1 创建DAL层        ※ 4.1.1 MySportsStore.IDAL详解        ※ 4.1.2 MySportsStore.DAL详解 1.搭建项目 MySportsStore.

ASP.NET_MVC4_使用ViewModel给前台传递多个model

先建立数据库并插入如下的SQL脚本和数据 CREATE TABLE Ta ( Id int identity(1,1) PRIMARY KEY , [TitleName] varchar(255), [Point11] varchar(255), [Point32] varchar(255), ) INSERT INTO [dbo].[Ta] ([TitleName] ,[Point11] ,[Point32] ) VALUES ('TAAAAAAAAA3' ,'2222.287042' ,'3

Google官方MVP模式示例项目解析 todo-mvp

转载请注明出处:http://www.cnblogs.com/cnwutianhao/p/6700668.html 引言:在Google没有给出一套权威的架构实现之前,很多App项目在架构方面都有或多或少的问题.第一种常见问题是没有架构,需求中的一个页面对应项目中的一个activity或一个fragment,所有的界面响应代码.业务逻辑代码.数据请求代码等等都集中在其中.第二种常见的问题是架构实现的不断变化,不断在各种架构间摇摆,一直找不到一个适合自己的架构. Google官方示例项目地址 ht

Android官方MVP架构示例项目解析

前段时间Google在Github推出了一个项目,专门展示Android引用各种各样的MVP架构,算是官方教程了.趁着还新鲜,让我们来抛砖引玉一探究竟,看看在Google眼里什么样算是好的MVP架构. App架构在Android开发者中一直是讨论比较多的一个话题,目前讨论较多的有MVP.MVVM.Clean这三种.google官方对于架构的态度一直是非常开放的,让开发者自主选择组织和架构app的方式,期望能留给开发者更多的灵活性. 由于没有一套权威的架构实现,现在很多App项目中在架构方面都有或

大数据精英实战项目班-Hadoop-Spark-真实企业项目

2018最新最全大数据技术视频,项目视频.整套视频,非那种杂七杂八自己拼凑的,内容如下,需要的联系QQ:3164282908(加Q注明大数据) 更有海量大数据技术视频.大数据项目视频,机器学习深度学习技术视频.项目视频.Python编程视频.Oracle数据库视频.Java培训视频高级架构师视频等等等. ├----------01-大数据Java基础------------- │├java第01天 ││├java第01天-01.类型转换.avi ││├java第01天-02.归档分析与实现.av

Asp.Net Core 入门(四)—— Model、View、Controller

和我们学习Asp.Net MVC一样,Asp.Net Core MVC的Model.View.Controller也和我们熟悉的Asp.Net MVC中的相似.不同的是我们在使用Asp.Net Core MVC的时候需要注入MVC. Asp.Net Core MVC注入 MVC 的方法有两种,一种是AddMvcCore(),它只是注入了MVC的一些核心的东西,不包括像返回的ViewResult.JsonResult等,如果要使用完整的MVC,需要使用另一种注入方式 AddMvc() 进行注入,A

『ENGLISH』

以A字母开头的词汇 英文 中文 abstract module 抽象模组 access 访问.存取 access control 存取控制 access control information 存取控制资讯 access mechanism 存取机制 access rights 存取权限 accessibility 无障碍性 accessibility information 无障碍网页资讯 accessibility problem 无障碍网页问题 accessible 无障碍的 access