参考资料: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',
));
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
));
1.
$objectResult=Post::model()->findAllByAttributes($attributes,$condition,$params);
2.
$objectResult=Post::model()->findAllByAttributes(array(
'username'
=>
'www.aipanshi.com'
));
1.
$arrResult=Post::model()->findAllBySql($sql,$params);
2.
$arrResult=Post::model()->findAllBySql(
"select * from tbl_post where username like :name"
,array(
':name'
=>
'%ad%'
));
1.
$arrResult=Post::model()->findByPk($postID,$condition,$params);
2.
$arrResult=Post::model()->findByPk(
1
);
1.
$arrRow=Post::model()->find($condition,$params);
2.
$arrRow=Post::model()->find(
'username=:name'
,array(
':name'
=>
'www.aipanshi.com'
));
1.
$objectResult=Post::model()->findByAttributes($attributes,$condition,$params);
2.
$objectResult=Post::model()->findByAttributes(array(
'username'
=>
'objectResult'
));
1.
$objectResult=Post::model()->findBySql($sql,$params);
2.
$objectResult=Post::model()->findBySql(
"select * from objectResult where username=:name"
,array(
':name'
=>
'objectResult'
));
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);
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'
,
1
,
4
);
// 等同于 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;
//是否唯一查询
1.
$intCount=Post::model()->count($condition,$params);
2.
$intCount=Post::model()->count(
"username=:name"
,array(
":name"
=>$username));
1.
$intCount=Post::model()->countBySql($sql,$params);
2.
$intCount=Post::model()->countBySql(
"select * from objectResult where username=:name"
,array(
':name'
=>
'objectResult'
));
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
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.
}}
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'
))