Yii 1.0数据库操作 查询、增加、更新、删除
1、根据条件查询一个集合
$objectResult=Post::model()->findAll($condition,$params);2、根据主键查询一个集合,可以使用多个主键 findAllByPk
$objectResult=Post::model()->findAll("username=:name",array(":name"=>$username));
$objectResult=RepairItem::model()->findAll("orderno=:orderno and orderpostid=:orderpostid",array(":orderno"=>$orderInfo['orderno'],':orderpostid'=>$orderInfo['orderpostid']));
$infoArr = NewsList::model()->findAll("status = '1' ORDER BY postid DESC limit 10 ");
$objectResult=Post::model()->findAllByPk($postIDs,$condition,$params);3、根据条件查询一个集合,可以是多个条件,把条件放到数组里面 findAllByAttributes
$objectResult=Post::model()->findAllByPk($postid,"name like :name and age=:age",array(':name'=>$name,'age'=>$age));
$objectResult=Post::model()->findAllByPk(array(1,2));
$objectResult=Post::model()->findAllByAttributes($attributes,$condition,$params);4、根据SQL语句查询一个数组 findAllBySql
$objectResult=Post::model()->findAllByAttributes(array('username'=>'jack'));
$arrResult=Post::model()->findAllBySql($sql,$params);5、根据主键查询出一个对象 eg:findByPk(1);
$arrResult=Post::model()->findAllBySql("select * from tbl_post where username like :name",array(':name'=>'?%'));
$arrResult=Post::model()->findByPk($postID,$condition,$params);6、根据条件查询出一组数据,【可能是多个,但是他只返回第一行数据】
$arrResult=Post::model()->findByPk(1);
$arrRow=Post::model()->find($condition,$params);7、根据条件查询一组数据,【可以是多个条件,把条件放到数组里面,查询的也是第一条数据】
$arrRow=Post::model()->find('username=:name',array(':name'=>'jack'));
$objectResult=Post::model()->findByAttributes($attributes,$condition,$params);8、根据SQL语句查询一组数据,【查询的也是第一条数据】
$objectResult=Post::model()->findByAttributes(array('username'=>'objectResult'));
$objectResult=Post::model()->findBySql($sql,$params);9、通过CDbCriteria类find查询出一个对象
$objectResult=Post::model()->findBySql("select * from objectResult where username=:name",array(':name'=>'objectResult'));
$criteria=new CDbCriteria;10、多条件查询的语句
$criteria->select='username'; // 限制显示哪些字段
$criteria->condition='username=:username'; //一个查询条件用aCondition.多条件用addCondition
$criteria->params=array(":username=>'jack'");
$criteria->order = "postsort DESC";
$criteria->limit = "3";
$post=Post::model()->find($criteria);
$criteria = new CDbCriteria;三、查询个数,判断查询是否有结果
$criteria->addCondition("postid=1"); //等同于 where postid = 1
$criteria->addInCondition('postid', array(1,2,3,4,5)); //等同于 where postid IN (1,2,3,4,5,);
$criteria->addNotInCondition('postid', array(1,2,3,4,5));//等同于 NOT IN (1,2,3,4,5,)
$criteria->addCondition('postid=1','OR');//等同于 OR而非AND
$criteria->addSearchCondition('username', 'jack');//等同于 where name like '%jack%'
$criteria->addBetweenCondition('postid', 1, 4);// 等同于 between 1 and 4
$criteria->compare('postid', 1); //根据你的参数自动处理成addCondition或者addInCondition.
$criteria->compare('postid', array(1,2,3)); //数组就会调用addInCondition
$criteria->select = 'postid,parentid,name'; //限制显示哪些字段
$criteria->join = 'xxx'; //连接表
$criteria->with = 'xxx'; //调用relations
$criteria->limit = 10; //取1条数据,如果小于0,则不作处理
$criteria->offset = 1; //两条合并起来,则表示 limit 10 offset 1,或者代表了。limit 1,10
$criteria->order = 'xxx DESC,XXX ASC' ;//排序条件
$criteria->group = 'group 条件';
$criteria->having = 'having 条件 ';
$criteria->distinct = FALSE; //是否唯一查询
四、添加的方法
根据一个条件查询一个集合有多少条记录,返回一个int型数字
$intCount=Post::model()->count($condition,$params);
$intCount=Post::model()->count("username=:name",array(":name"=>$username));
根据SQL语句查询一个集合有多少条记录,返回一个int型数字
$intCount=Post::model()->countBySql($sql,$params);
$intCount=Post::model()->countBySql("select * from objectResult where username=:name",array(':name'=>'objectResult'));
根据一个条件查询查询得到的数组有没有数据,有数据返回一个true,否则没有找到
$boolExists=Post::model()->exists($condition,$params);
$boolExist=Post::model()->exists("name=:name",array(":name"=>$username));
$objectPost = new Post;五、修改的方法
$objectPost->username = $username;
$objectPost->password = $password;
或许
$objectPost->attributes = $arrNewData;
if($objectPost->save()){
$intPostId= $objectPost->primaryKey; //生成主键id
echo "添加成功";
}else{
echo "添加失败";
}
六、删除的方法
Post::model()->updateAll($attributes,$condition,$params);
$count =Post::model()->updateAll(array('username'=>'11111','password'=>'11111'),'password=:pass',array(':pass'=>'1111a1'));
if($count > 0){
echo "修改成功";
}else{
echo "修改失败";
}
$rt = PostList::model()->updateAll(array('status'=>'1'),'staff_postid=:staff AND host_postid=:host',array(':staff'=>$staff_postid,':host'=>$host_postid));
Post::model()->updateByPk($pk,$attributes,$condition,$params);
$count=Post::model()->updateByPk(1,array('username'=>'jack','password'=>'jack'));
$count=Post::model()->updateByPk(array(1,2),array('username'=>'jack1','password'=>'jack1'),'username=:name',array(':name'=>'jack'));
if($count > 0){
echo "修改成功";
}else{
echo "修改失败";
}
Post::model()->updateCounters($counters,$condition,$params);
$count=Post::model()->updateCounters(array('status'=>1),'username=:name',array(':name'=>'jack'));
if($count > 0){
echo "修改成功";
}else{
echo "修改失败";
}
//array('status'=>1)代表数据库中的post表根据条件username='jack',查询出的所有结果status字段都自加1
//deleteAll七、执行原生的SQL语句
Post::model()->deleteAll($condition,$params);
$count = Post::model()->deleteAll('username=:name and password=:pass',array(':name'=>'jack',':pass'=>'jack'));
$count = Post::model()->deleteAll('postid in("1,2,3")');//删除postid为这些的数据
if($count>0){
echo "删除成功";
}else{
echo "删除失败";
}
//deleteByPk
Post::model()->deleteByPk($pk,$condition,$params);
$count = Post::model()->deleteByPk(1);
$count =Post::model()->deleteByPk(array(1,2),'username=:name',array(':name'=>'jack'));
if($count>0){
echo "删除成功";
}else{
echo "删除失败";
}}
$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)";八、事务处理 【多表更新插入操作请使用事务处理】
$arrRows=Yii::app()->db->createCommand($sql)->query();
foreach ($arrRows as $k => $v){
}
$transaction = Yii::app()->db->beginTransaction();
try{
$arrOrderProfile = array(
'orderid' => $orderId,
'userip' => $userip,
'contactid' => $contactId,
'updatetime'=> $now
);
$modelOrderProfile = new RepairOrderProfile();
$modelOrderProfile->attributes = $arrOrderProfile;
if(!$modelOrderProfile->save()){
throw new CException('维修订单生成失败,通知事务回滚');
}
$recordCounter = Counter::model()->updateByPk(1,array('max_id'=>$orderId));
if($recordCounter <= 0 )
throw new CException('订单计数器更新失败,通知事务回滚');
$transaction->commit(); //提交事务会真正的执行数据库操作
}catch(Exception $e){
file_put_contents('action.log', $e->getCode().':'.$e->getMessage().'--'.date('Y-m-d H:i:s',time()),FILE_APPEND);
$transaction->rollback();
}