thinkphp 3.2 模型的使用示例
2014-10-14 17:09
651 查看
原来以为thinkPHP的 model 就和PHPCMS一样 就起到一个连接数据库的作用,今天看了视频,才发现这个也是 mvc中的m
使用方法可以使用 D() 方法
下面是 UserController
UserModel
使用方法可以使用 D() 方法
下面是 UserController
<?php namespace Home\Controller; use Think\Controller; 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 \Think\Verify(); return $verify->check($code, $id); } **/ }
UserModel
<?php namespace Home\Model; use Think\Model; 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 \Think\Verify(); return $verify->check($code, $id); } } ?>
在本模型中 $this 就等于 M("本模型");
$this的效率要高于M();
$this的出错率比较低;
相关文章推荐
- ThinkPHP学习笔记(五)使用模型操作数据库之CURD
- ThinkPHP 3.1.2 输出和模型使用 配置项等 - 2
- thinkphp的c方法使用示例
- yii框架表单模型使用及以数组形式提交表单数据示例
- yii框架表单模型使用及以数组形式提交表单数据示例
- thinkphp框架函数switchModel(调用动态模型时使用)
- [ThinkPHP] 输出、模型的使用
- thinkphp控制器调度使用示例
- 使用thinkphp3.2中的验证码功能
- ThinkPHP第十一天(关联模型使用,独立分组配置,MySQL concat用法)
- thinkPHP模板的输出和模型的使用
- thinkPHP模板的输出和模型的使用
- thinkphp控制器调度使用示例
- thinkphp3.2上传方法使用教程
- ThinkPHP 3.1.2 输出 模型使用 配置
- easyUI+ThinkPHP使用扩展datagrid-filter简单示例
- thinkphp3.2开发排错一例——D方法与M方法的使用
- Thinkphp 3.2使用小技巧
- ThinkPHP的输出和模型使用