码迷,mamicode.com
首页 > 其他好文 > 详细

tp AR

时间:2016-06-08 10:35:55      阅读:310      评论:0      收藏:0      [点我收藏+]

标签:

select:普通查询,同 findAll() 方法
find:取得符合查询条件的一条记录


$list = $Dao->field(username,email)->select();    //查找某个字段

//where  等于
$condition[username] = Admin;
$map[a] =array(like,array(%thinkphp%,%tp),OR);  //like
$map[id]  = array(between,1,8);      //between
$map[id]  = array(not in,1,5,8);     //in  / not in
$map[id]  = array(not in,array(1,5,8));
$map[name|title] = aa;    //or  (name=aa or title = aa)

//‘_multi‘=>true必须加在数组的最后,表示当前是多条件匹配 
//查询条件就变成 status= 1 AND score >0 AND title = ‘thinkphp‘
$map[status&score&title] =array(1,array(gt,0),thinkphp,_multi=>true);  //and  

//区间
$map[id] = array(array(gt,1),array(lt,10)) ;   // (`id` > 1) AND (`id` < 10)
$map[id] = array(array(gt,3),array(lt,10), or) ;   // (`id` > 3) OR (`id` < 10)
$map[id]  = array(array(neq,6),array(gt,3),and);   //(`id` != 6) AND (`id` > 3)
$map[name]  = array(array(like,%a%), array(like,%b%), array(like,%c%), ThinkPHP,or);  
//(`name` LIKE ‘%a%‘) OR (`name` LIKE ‘%b%‘) OR (`name` LIKE ‘%c%‘) OR (`name` = ‘ThinkPHP‘)


$list = $Dao->where($condition)->select();

$list = $Dao->order(uid DESC)->select();

$list = $Dao->limit(4,5)->select();

$list = $Dao->order(uid DESC)->limit(4,5)->select();


---------------
//保存
$User = M("User"); // 实例化User对象
// 要修改的数据对象属性赋值
$data[name] = ThinkPHP;
$data[score] = array(exp,score+1);// 用户的积分加1
$User->where(id=5)->save($data);

//_string 包括字符串模式查询
$User = M("User"); // 实例化User对象
$map[id] = array(neq,1);
$map[name] = ok;
$map[_string] = status=1 AND score>10;
$User->where($map)->select(); 

//请求字符串查询(_query)
$map[id] = array(gt,100);
$map[_query] = status=1&score=100&_logic=or;  //`id`>100 AND (`status` = ‘1‘ OR `score` = ‘100‘)

//Count() ,Max() ,Min(),Avg(),Sum()
$User = M("User");
$userCount = $User->count();   //所有用户数
$userCount = $User->count("id");
$minScore = $User->where(score>0)->min(score);  //获取积分大于0的用户的最小积分

$User->where(score>80)->order(score desc)->first();  //获取第一条记录
$User->where(score>80)->order(score desc)->last();  //获取最后一条记录
$User-> where(score>80)->order(score desc)->top8();   //前8条数据

//动态查询
//getBy+表中属性的名  
$user = $User->getByName(liu21st);
//根据名子获得id
$user = $User->getFieldByName(liu21st,id);

//sql查询
$Model = new Model() // 实例化一个model对象 没有对应任何数据表
$Model->query("select * from think_user where status=1");

//修改
$Model = new Model() // 实例化一个model对象 没有对应任何数据表
$Model->execute("update think_user set name=‘thinkPHP‘ where status=1");

$data[email] = Jack@163.com;
$data[uid] = 2;
$result = $Dao->save($data);

//删除
$result = $Dao->where(uid = 5)->delete();
$result = $Dao->where(status=0)->order(regdate ASC)->limit(5)->delete();    //删除多条

 

tp AR

标签:

原文地址:http://www.cnblogs.com/suxiaolong/p/5569065.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!