在ThinkPHP中基础的模型类就是Model类,该类完成了基本的CURD、ActiveRecord模式、连贯操作和统计查询,一些高级特性被封装到另外的模型扩展中。
模型定义
模型类一般位于项目的Lib/Model 目录下面,当我们创建一个UserModel类的时候,其实已经遵循了系统的约定。模型类的命名规则是除去表前缀的数据表名称,采用驼峰法命名,并且首字母大写,然后加上模型类的后缀定义Model,例如:模型名(类名) 约定对应数据表(假设数据库的前缀定义是 think_)
UserModel think_user
UserTypeModel think_user_type
如果你的规则和上面的系统约定不符合,那么需要设置Model类的数据表名称属性。
自定义模型的使用
1.有自定义模型的时候
$goodsob
$goodsob
$goodsob
2.未定义自定义模型的时候
$goodsob
CURD
1. IS_POST IS_GET IS_AJAX
2. CURD的C
create()
3. CURD的U
save()
add()
setField();//编辑单个字段
setInc(); //增大字段
setDec(); //减小字段
4. CURD的R
select();
find();
getField();//获取单个字段
5.CURD的D
delete();
连贯操作
连贯操作 作用 支持的参数类型
where 用于查询或者更新条件的定义 字符串、数组和对象
$obj->where(array(\\\'id\\\'=>$id))->find(); || $obj->where(\\\'id>\\\'.$id)->select();
field 用于定义要查询的字段(支持字段排除) 字符串和数组
$obj->where(array(\\\'id\\\'=>$id))->field(\\\'id,name,goods_sn\\\')->find();$obj->where(array(\\\'id\\\'=>$id))->field(\\\'id,name\\\')->select();
order 用于对结果排序 字符串和数组
$obj->where(array(\\\'id\\\'=>$id))->field(\\\'id,name\\\')->order(\\\'id DESC\\\')->select();
$obj->where(array(\\\'id\\\'=>$id))->field(\\\'id,name\\\')->order(\\\'id ASC\\\')->select();
limit 用于限制查询结果数量 字符串和数字
$obj->where(array(\\\'id\\\'=>$id))->field(\\\'id,name\\\')->order(\\\'id ASC\\\')->limit(5)->select();
$obj->where(array(\\\'id\\\'=>$id))->field(\\\'id,name\\\')->order(\\\'id ASC\\\')->limit(5,5)->select();
http://www.phpteahouse.com/video/view/40.html
最佳答案