日期:2014-05-16 浏览次数:20506 次
?
=======================================
??? public function getMinLimit () {
??????? $sql = "...";
??????? $result = yii::app()->db->createCommand($sql);
??????? $query = $result->queryAll();
??????? return array (
??????????????? $query [0] ['max'],
??????? );
??? }
?
?
?
$connection=Yii::app()->db;
$sql="SELECT u.account,i.* FROM sys_user as u left join user_info as i on u.id=i.user_id";
$rows=$connection->createCommand
($sql)->query();
foreach ($rows as $k => $v ){
??? echo $v['add_time'];
}
?
?
======================================== 事务
$db = Yii::app()->db;
$dbTrans = $db->beginTransaction();
$dbTrans->commit();
$dbTrans->rollback();
?
?
?
========================================? 分页
?
一般写法:
$criteria = new CDbCriteria;
$count = DnOnline::model()->count($criteria);
$pages = new CPagination($count);??????????????
$pages->pageSize = 5;
$pages->applylimit($criteria);
$model = DnOnline::model()->findAll($criteria);
记得控制器里有个函数paginate($itemCount,$pageSize=null,$pageVar=null)
CDB写法:
$criteria = new CDbCriteria;
$sql = "SELECT * FROM USER";
$model= Yii::app()->db->createCommand($sql)->queryAll();
$pages = new CPagination(count($model));??????????????
$pages->pageSize = 4;
$pages->applylimit($criteria);
$model=Yii::app()->db->createCommand($sql." LIMIT :offset,:limit");
$model->bindValue(':offset', $pages->currentPage*$pages->pageSize);
$model->bindValue(':limit', $pages->pageSize);
$model=$model->queryAll();
或者:
????? $criteria = new CDbCriteria;
????? $connection=Yii::app()->db;
????? $sql="SELECT u.account,i.* FROM sys_user as u left join user_info as i on u.id=i.user_id ";
????? $model= Yii::app()->db->createCommand($sql)->queryAll();
????? $pages = new CPagination(count($model));??????????????
????? $pages->pageSize = 4;
????? $pages->applylimit($criteria);
????? $model=Yii::app()->db->createCommand($sql." LIMIT :offset,:limit");
????? $model->bindValue(':offset', $pages->currentPage*$pages->pageSize);
????? $model->bindValue(':limit', $pages->pageSize);
????? $model=$model->queryAll();
?
?
======================================== 一、查询数据集合
1、$admin=Admin::model()->findAll
($condition,$params);
该方法是根据一个条件查询一个集合,如:?
findAll("username=:name",array(":name"=>$username));????
2、$admin=Admin::model()->findAllByPk($postIDs,$condition,$params);
findAllByPk($id,"name like ':name' and age=:age",array(':name'=>$name,'age'=>$age));???
该方法是根据主键查询一个集合,可以使用多个主键,如:?
findAllByPk(array(1,2));???
3、$admin=Admin::model()->findAllByAttributes($attributes,$condition,$params);
该方法是根据条件查询一个集合,可以是多个条件,把条件放到数组里面,如:?
findAllByAttributes(array('username'=>'admin'));
4、$admin=Admin::model()->findAllBySql($sql,$params);
该方法是根据SQL语句查询一个数组,如:
findAllBySql("select *from admin whereusername=:name",array(':name'=>'admin'));
========================================? 二、查询对像
的方法?
1、$admin=Admin::model()->findByPk
($postID,$condition,$params);
?根据主键查询出一个对象,如:findByPk(1);?
?
?
?
?