一、查询数据
1、findAll(根据一个条件查询一个集合)
$admin=Admin::model()- findAll($condition,$params);$admin=Admin::model()- findAll( username=:name ,array( :name = $username));$admin=Admin::model()- findAll(“username=:name and age=:age” , array(“:name”= $name, “age”= $age));$admin=Admin::model()- findAll(“username like :name and age=:age” , array(“:name”= $name, “age”= $age));$infoArr= NewsList::model()- findAll( status = 1 ORDER BY id DESC limit 10
2、findAllByPk(根据主键查询一个集合,可以使用多个主键)
$admin=Admin::model()- findAllByPk($postIDs,$condition,$params);$admin=Admin::model()- findAllByPk($id, name like :name and age=:age ,array( :name = $name, age = $age));$admin=Admin::model()- findAllByPk(array(1,2));
3、findAllByAttributes(根据条件查询一个集合,可以是多个条件,把条件放到数组里面)
$admin=Admin::model()- findAllByAttributes($attributes,$condition,$params);$admin=Admin::model()- findAllByAttributes(array( username = admin
4、findAllBySql(根据SQL语句查询一个数组)
$admin=Admin::model()- findAllBySql($sql,$params);$admin=Admin::model()- findAllBySql( select * from admin where username like :name ,array( :name = %ad%
5、findByPk(根据主键查询出一个对象)
$admin=Admin::model()- findByPk($postID,$condition,$params);$admin=Admin::model()- findByPk(1);
6、find(根据一个条件查询出一组数据,可能是多个,只返回第一行数据)
$row=Admin::model()- find($condition,$params);$row=Admin::model()- find( username=:name ,array( :name = admin
7、findByAttributes(根据条件查询一组数据,可以是多个条件,把条件放到数组里面,查询第一条数据)
$admin=Admin::model()- findByAttributes($attributes,$condition,$params);$admin=Admin::model()- findByAttributes(array( username = admin
8、findBySql(根据SQL语句查询一组数据,查询第一条数据)
$admin=Admin::model()- findBySql($sql,$params);$admin=Admin::model()- findBySql( select * from admin where username=:name ,array( :name = admin
9、count(根据一个条件查询一个集合有多少条记录,返回一个int型数字)
$count=Post::model()- count($condition,$params);$count=Post::model()- count( username=:name ,array( :name = $username));
10、countBySql(根据SQL语句查询一个集合有多少条记录,返回一个int型数字)
$count=Post::model()- countBySql($sql,$params);$count=Post::model()- countBySql( select * from admin where username=:name ,array( :name = admin
11、exists(根据一个条件查询查询得到的数组有没有数据,如果有数据返回一个true,否则没有找到)
$exists=Post::model()- exists($condition,$params);$exists=Post::model()- exists( name=:name ,array( :name = $username));
二、添加数据
save(添加数据)
$admin=new Admin; $admin- username =$username;$admin- password =$password;if($admin- save() 0){echo 添加成功 }else{echo 添加失败 }
三、修改数据
update($pk主键,可以是一个也可以是一个集合,$attributes是要修改的字段的集合,$condition条件,$params传入的值)
Post::model()- updateAll($attributes,$condition,$params);$count=Admin::model()- updateAll(array( username = 11111 , password = 11111 ), password=:pass ,array( :pass = 1111a1 if($count 0){echo 修改成功 }else{echo 修改失败 }$result=PostList::model()- updateAll(array( status = 1 ), staff_id=:staff and host_id=:host ,array( :staff = $staff_id, :host = $host_id))
Post::model()- updateByPk($pk,$attributes,$condition,$params);$count=Admin::model()- updateByPk(1,array( username = admin , password = admin $count=Admin::model()- updateByPk(array(1,2),array( username = admin , password = admin ), username=:name ,array( :name = admin if($count 0){echo 修改成功 }else{echo 修改失败 }Post::model()- updateCounters($counters,$condition,$params);$count=Admin::model()- updateCounters(array( status = 1), username=:name ,array( :name = admin if($count 0){echo 修改成功 }else{echo 修改失败 }
array( status = 1)代表数据库中的admin表根据条件username= admin ,查询出的所有结果status字段都自加1
四、删除数据
delete
Post::model()- deleteAll($condition,$params);$count=Admin::model()- deleteAll( username=:nameandpassword=:pass ,array( :name = admin , :pass = admin $count= Admin::model()- deleteAll( id in( 1,2,3 ) //删除id为这些的数据if($count 0){echo 删除成功 }else{echo 删除失败 }Post::model()- deleteByPk($pk,$condition,$params);$count= Admin::model()- deleteByPk(1);$count=Admin::model()- deleteByPk(array(1,2), username=:name ,array( :name = admin if($count 0){echo 删除成功 }else{echo 删除失败 }
五、createCommand
$sql= SELECT u.account,i.* FROM sys_user as u left join user_info as i on u.id=i.user_id $rows=Yii::app()- db- createCommand($sql)- query();foreach($rowsas $k = $v){ echo$v[ add_time }
六、事务处理
$dbTrans= Yii::app()- db- beginTransaction();try{ $post=new Post; $post- title = Hello dodobook!!! if(!$post- save()){throw new Exception( Error Processing Request , 1); $dbTrans- commit(); $this- _end(0, 添加成功!!! }catch(Exception$e){ $dbTrans- rollback(); $this- _end($e- getCode(),$e- getMessage());}
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP !
相关推荐:
PHP让数组中有相同值的组成新的数组实例讲解
解决PHP字符串长度不一致的问题详解
Laravel5.2使用Captcha生成验证码实现登录
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。
新闻热点
疑难解答