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(
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
copyright © 2008-2019 亿联网络 版权所有 备案号:粤ICP备14031511号-2