Yii 1.0数据库操作 查询、增加、更新、删除

参考资料:http://www.aipanshi.com/post/2.html


1、根据条件查询一个集合

1. $objectResult=Post::model()->findAll($condition,$params);
2. $objectResult=Post::model()->findAll("username=:name",array(":name"=>$username));
3. $objectResult=RepairItem::model()->findAll("orderno=:orderno and orderpostid=:orderpostid",array(":orderno"=>$orderInfo['orderno'],':orderpostid'=>$orderInfo['orderpostid']));
4.  
5. $infoArr = NewsList::model()->findAll("status = '1' ORDER BY postid DESC limit 10 ");

//查询是使用distinct字段去除指定字段的重复记录
$sites = Post::model()->findAll(array(
    'select'=>array('distinct did','nodeid','site'),
    'order'=>'id ASC',
));
2、根据主键查询一个集合,可以使用多个主键 findAllByPk
1. $objectResult=Post::model()->findAllByPk($postIDs,$condition,$params);
2. $objectResult=Post::model()->findAllByPk($postid,"name like :name and age=:age",array(':name'=>$name,'age'=>$age));
3. $objectResult=Post::model()->findAllByPk(array(1,2));
3、根据条件查询一个集合,可以是多个条件,把条件放到数组里面 findAllByAttributes
1. $objectResult=Post::model()->findAllByAttributes($attributes,$condition,$params);
2. $objectResult=Post::model()->findAllByAttributes(array('username'=>'www.aipanshi.com'));
4、根据SQL语句查询一个数组 findAllBySql
1. $arrResult=Post::model()->findAllBySql($sql,$params);
2. $arrResult=Post::model()->findAllBySql("select * from tbl_post where username like :name",array(':name'=>'%ad%'));
5、根据主键查询出一个对象 eg:findByPk(1);
1. $arrResult=Post::model()->findByPk($postID,$condition,$params);
2. $arrResult=Post::model()->findByPk(1);
6、根据条件查询出一组数据,【可能是多个,但是他只返回第一行数据】
1. $arrRow=Post::model()->find($condition,$params);
2. $arrRow=Post::model()->find('username=:name',array(':name'=>'www.aipanshi.com'));
7、根据条件查询一组数据,【可以是多个条件,把条件放到数组里面,查询的也是第一条数据】
1. $objectResult=Post::model()->findByAttributes($attributes,$condition,$params);
2. $objectResult=Post::model()->findByAttributes(array('username'=>'objectResult'));
8、根据SQL语句查询一组数据,【查询的也是第一条数据】
1. $objectResult=Post::model()->findBySql($sql,$params);
2. $objectResult=Post::model()->findBySql("select * from objectResult where username=:name",array(':name'=>'objectResult'));
9、通过CDbCriteria类find查询出一个对象
1. $criteria=new CDbCriteria;
2. $criteria->select='username'// 限制显示哪些字段
3. $criteria->condition='username=:username';     //一个查询条件用aCondition.多条件用addCondition
4. $criteria->params=array(":username=>'www.aipanshi.com'");
5. $criteria->order = "postsort DESC";
6. $criteria->limit = "3";
7. $post=Post::model()->find($criteria);
10、多条件查询的语句
01. $criteria = new CDbCriteria;    
02. $criteria->addCondition("postid=1"); //等同于 where postid = 1
03. $criteria->addInCondition('postid', array(1,2,3,4,5)); //等同于 where postid IN (1,2,3,4,5,);
04. $criteria->addNotInCondition('postid', array(1,2,3,4,5));//等同于 NOT IN (1,2,3,4,5,)
05. $criteria->addCondition('postid=1','OR');//等同于 OR而非AND
06. $criteria->addSearchCondition('username''www.aipanshi.com');//等同于 where name like '%www.aipanshi.com%'
07. $criteria->addBetweenCondition('postid'14);// 等同于 between 1 and 4
08. $criteria->compare('postid'1);    //根据你的参数自动处理成addCondition或者addInCondition.
09. $criteria->compare('postid', array(1,2,3));   //数组就会调用addInCondition
10.  
11.  
12. $criteria->select = 'postid,parentid,name'//限制显示哪些字段
13. $criteria->join = 'xxx'//连接表
14. $criteria->with = 'xxx'//调用relations 
15. $criteria->limit = 10;    //取1条数据,如果小于0,则不作处理
16. $criteria->offset = 1;   //两条合并起来,则表示 limit 10 offset 1,或者代表了。limit 1,10
17. $criteria->order = 'xxx DESC,XXX ASC' ;//排序条件
18. $criteria->group = 'group 条件';
19. $criteria->having = 'having 条件 ';
20. $criteria->distinct = FALSE; //是否唯一查询
三、查询个数,判断查询是否有结果 根据一个条件查询一个集合有多少条记录,返回一个int型数字
1. $intCount=Post::model()->count($condition,$params);
2. $intCount=Post::model()->count("username=:name",array(":name"=>$username));
根据SQL语句查询一个集合有多少条记录,返回一个int型数字
1. $intCount=Post::model()->countBySql($sql,$params);
2. $intCount=Post::model()->countBySql("select * from objectResult where username=:name",array(':name'=>'objectResult'));
根据一个条件查询查询得到的数组有没有数据,有数据返回一个true,否则没有找到
1. $boolExists=Post::model()->exists($condition,$params);
2. $boolExist=Post::model()->exists("name=:name",array(":name"=>$username));
四、添加的方法
01. $objectPost = new Post;     
02. $objectPost->username = $username;
03. $objectPost->password = $password;
04. 或许
05. $objectPost->attributes = $arrNewData;
06.  
07. if($objectPost->save()){
08. $intPostId= $objectPost->primaryKey; //生成主键id
09. echo "添加成功";
10. }else{
11. echo "添加失败";
12. }
五、修改的方法
01. Post::model()->updateAll($attributes,$condition,$params);
02. $count =Post::model()->updateAll(array('username'=>'11111','password'=>'11111'),'password=:pass',array(':pass'=>'1111a1'));
03. if($count > 0){
04. echo "修改成功";
05. }else{
06. echo "修改失败";
07. }
08.  
09. $rt = PostList::model()->updateAll(array('status'=>'1'),'staff_postid=:staff AND host_postid=:host',array(':staff'=>$staff_postid,':host'=>$host_postid));
10.  
11. /* $pk主键 也可以是一个集合
12. * $attributes是要修改的字段的集合
13. * $condition条件
14. * $params传入的值
15. */
16. Post::model()->updateByPk($pk,$attributes,$condition,$params);
17.  
18. $count =Post::model()->updateByPk(1,array('username'=>'www.aipanshi.com','password'=>'www.aipanshi.com'));
19.  
20. $count =Post::model()->updateByPk(array(1,2),array('username'=>'www.aipanshi.com1','password'=>'www.aipanshi.com1'),'username=:name',array(':name'=>'www.aipanshi.com'));
21.  
22. if($count > 0){
23. echo "修改成功";
24. }else{
25. echo "修改失败";
26. }
27.  
28. Post::model()->updateCounters($counters,$condition,$params);
29.  
30. $count=Post::model()->updateCounters(array('status'=>1),'username=:name',array(':name'=>'www.aipanshi.com'));
31.  
32. if($count > 0){
33. echo "修改成功";
34. }else{
35. echo "修改失败";
36. }
37.  
38. //array('status'=>1)代表数据库中的post表根据条件username='www.aipanshi.com',查询出的所有结果status字段都自加1
六、删除的方法 //deleteAll
01. Post::model()->deleteAll($condition,$params);
02. $count = Post::model()->deleteAll('username=:name and password=:pass',array(':name'=>'www.aipanshi.com',':pass'=>'www.aipanshi.com'));
03. $count = Post::model()->deleteAll('postid in("1,2,3")');//删除postid为这些的数据
04. if($count>0){
05. echo "删除成功";
06. }else{
07. echo "删除失败";
08. }
09.  
10. //deleteByPk
11. Post::model()->deleteByPk($pk,$condition,$params);
12. $count = Post::model()->deleteByPk(1);
13. $count =Post::model()->deleteByPk(array(1,2),'username=:name',array(':name'=>'www.aipanshi.com'));
14. if($count>0){
15. echo "删除成功";
16. }else{
17. echo "删除失败";
18. }}
七、执行原生的SQL语句
1. $sql = "select t.*, t1.userphone, t1.truename, t1.usermail from {{member_contact}} t left join {{member}} t1 on t.userid = t1.userid where t.contactid in (1,2,3)";
2. $arrRows=Yii::app()->db->createCommand($sql)->query();
3. foreach ($arrRows as $k => $v){
4. ...
5. }

八、一些零零碎碎的收集

//查询出复合条件的用户数组,指定了index以后,结果数组$users已经是用id做为主键的了

1. $users = User::model()->findAll(array('condition'=>'age > 18''index'=> 'id'))

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值