zoukankan      html  css  js  c++  java
  • thinkphp 3.2 模型的使用示例

    原来以为thinkPHP的 model 就和PHPCMS一样  就起到一个连接数据库的作用,今天看了视频,才发现这个也是 mvc中的m

    使用方法可以使用 D() 方法

    下面是 UserController

    <?php
    namespace HomeController;
    use ThinkController;
    class UserController extends Controller {
    	/**-- 用户中心 --**/
        public function index(){
            $this->display();
        }
    	/**-- 登陆页面 --**/
    	public function login(){
    		$this->display();
        }
    	/**-- 执行登陆操作 --**/
    	public function dologin(){
    		$data = I('post.');
    		$result = D('User')->login($data);
    		var_dump($result);
    	}
    	/**-- 验证验证码 --
    	private function check_verify($code, $id = ''){    
    		$verify = new ThinkVerify();    
    		return $verify->check($code, $id);
    	}
    	**/
    }
    

     UserModel

    <?php
    namespace HomeModel;
    use ThinkModel;
    class UserModel extends Model {
    	/**-- 登陆 --**/
    	public function login($data){
    		if(!($this->check_verify($data['code']))){
    			return '验证码错误';
    		}
    		if($data['name'] == '' || $data['password'] == ''){
    			return '用户名或密码不能为空!';
    		}
    		$user = M('User');
    		$data['password'] = md5($data['password']);
    		$result = $user->where(array('name'=>$data['name'],'password'=>$data['password']))->find();
    		if($result){
    			return '欢迎您 '.$result['name'];
    		}else{
    			return '没有该用户';
    		}
    	}
    	/**-- 验证验证码 --**/
    	private function check_verify($code, $id = ''){    
    		$verify = new ThinkVerify();    
    		return $verify->check($code, $id);
    	}
    }
    ?>
    

    在本模型中  $this 就等于 M("本模型");

    $this的效率要高于M();

    $this的出错率比较低;

  • 相关阅读:
    按键精灵-3-按键精灵键盘鼠标命令
    Python-13-pass,del和exec
    STP-9-处理RSTP中的拓扑变化
    Python-12-简单推导
    Python-11-循环
    Python-10-条件和条件语句
    Qt qml listview 列表视图控件(下拉刷新、上拉分页、滚动轴)
    Qt qml pageview 左右滑动分页组件
    Qt qml treeview 树控件
    Qt qml 模拟iphone slide to unlock 的聚光动画文字效果
  • 原文地址:https://www.cnblogs.com/mr-amazing/p/4024812.html
Copyright © 2011-2022 走看看