array:findAllBySql() 方法 CActiveRecord:findBySql() KO
$goods=Goods::model()->findAllBySql('select * from tb_goods where id=:c',array(':c'=>11));
array:findAllByAttributes()与 CActiveRecord :findByAttributes()
$goods=Goods::model()->findAllByAttributes(array('goods_name'=>15));
find()和 findAll()
//参数可以为(contidion字符窜,params数组(对条件字符窜赋值))
//也可以是(contidion字符窜)
//也可以是(arr(xxxxxxx)),其中arr作为CDbCriteria对象初始值
//也可以是(CDbCriteria对象);
$goods=Goods::model()->find(array('condition'=>'id=:gid','params'=>array(':gid'=>11)));
$goods=Goods::model()->find('id=:gid and goods_name =:c',array(':gid'=>11,':c'=>'老豆腐'));
$goods=Goods::model()->find("id=14");
//findByAttributes() 与 findAllByAttributes() 方法
$goods=Goods::model()->findByAttributes(array('id'=>11,'goods_name'=>'老豆腐'));
//findbyPk
$goods=Goods::model() -> findByPk(12);
添加的方法
$admin=newAdmin;
$admin->username=$username;
$admin->password=$password;
if($admin->save ()>0){
echo "添加成功";
}else{
echo "添加失败";
}
$userLimit = new UserLimit();
$userLimit->item = 0.30000;
if($userLimit->insert (){
}
修改的方法
$userLimitRet = UserLimit::model()->findByPk (array (
'user_id' => $userId,
'category_id' => $v
));
$userLimitRet->order = $order;
if (! $userLimitRet->update ()) {
}
$userAmount=userAmount::model()->findByPk ($userId);
$userAmount->credit=Yii::app()->request->getParam('credit',10000);
$ret = $userAmount->save ();
1、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 "修改失败";
}
$ret = $this->updateAll (array (
'set' => $Set
), " user_id in ( " . $userIds . " ) and category_id = " . $categoryId);
if($ret){
return true;
}else {
return false;
}
2、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 "修改失败";
}
$pk代表主键,可以是一个也可以是一个集合,$attributes代表是要修改的字段的集合,$condition代表条件,$params传入的值
3、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
、删除的方法
1、Post::model()->deleteAll($condition,$params);
$count = Admin::model()->deleteAll('username=:nameandpassword=:pass',array(':name'=>'admin',':pass'=>'admin'));
$id=1,2,3
deleteAll('id in(".$id.")');删除id为这些的数据
if($count>0){
echo "删除成功";
}else{
echo "删除失败";
}
2、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 "删除失败";
}