Yii 框架裏數據庫操做詳解-[增長、查詢、更新、刪除的方法 'AR模式']


    public function getMinLimit () {
        $sql = "...";
        $result = yii::app()->db->createCommand($sql);
        $query = $result->queryAll();
 
        return array (
                $query [0] ['max'], 
        );
    }sql

 

 

 



$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();app


$dbTrans->commit();
$dbTrans->rollback();yii

 

 

 

========================================  分頁函數

 


通常寫法:
$criteria = new CDbCriteria;
$count = DnOnline::model()->count($criteria);
$pages = new CPagination($count);               
$pages->pageSize = 5;
$pages->applylimit($criteria);
$model = DnOnline::model()->findAll($criteria);post

記得控制器裏有個函數paginate($itemCount,$pageSize=null,$pageVar=null)
CDB寫法:ui


$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();


this

 

 

======================================== 1、查詢數據集合 

一、$admin=Admin::model()->findAll ($condition,$params);
該方法是根據一個條件查詢一個集合,如:  
findAll("username=:name",array(":name"=>$username));    spa


二、$admin=Admin::model()->findAllByPk($postIDs,$condition,$params); 
findAllByPk($id,"name like ':name' and age=:age",array(':name'=>$name,'age'=>$age));    
該方法是根據主鍵查詢一個集合,能夠使用多個主鍵,如:  
findAllByPk(array(1,2));   


三、$admin=Admin::model()->findAllByAttributes($attributes,$condition,$params); 
該方法是根據條件查詢一個集合,能夠是多個條件,把條件放到數組裏面,如:  
findAllByAttributes(array('username'=>'admin'));


四、$admin=Admin::model()->findAllBySql($sql,$params); 
該方法是根據SQL語句查詢一個數組,如: 
findAllBySql("select *from admin whereusername=:name",array(':name'=>'admin')); 

========================================  2、查詢對像 的方法  

一、$admin=Admin::model()->findByPk ($postID,$condition,$params); 
 根據主鍵查詢出一個對象,如:findByPk(1); 

 

 

 

 


 二、$row=Admin::model()->find ($condition,$params); 
 根據一個條件查詢出一組數據,多是多個,可是他只返回第一行數據,如: 
find('username=:name',array(':name'=>'admin'));

 

      $userinfo=Userinfo::model()->find(array('condition'=>'user_id=:user_id','params'=>array(':user_id'=>$uid)));
      print_r($userinfo->user_id);

 

 


 三、$admin=Admin::model()->findByAttributes ($attributes,$condition,$params); 
 該方法是根據條件查詢一組數據,能夠是多個條件,把條件放到數組裏面,他查詢的也是第一條數據,如: 
findByAttributes(array('username'=>'admin'));


 四、$admin=Admin::model()->findBySql ($sql,$params); 
 該方法是根據SQL語句查詢一組數據,他查詢的也是第一條數據,如:  
findBySql("select *from admin whereusername=:name",array(':name'=>'admin'));


 五、拼一個得到SQL的方法,在根據find查詢出一個對象 


$criteria=new CDbCriteria;  
$criteria->select='username'; // only select the 'title' column  
$criteria->condition='username=:username'; 
$criteria->params=array(':username=>'admin'); 
$post=Post::model()->find($criteria); // $params isnot needed  
 
========================================  3、查詢個數,判斷查詢是否有結果  

一、$n=Post::model()->count ($condition,$params); 
 該方法是根據一個條件查詢一個集合有多少條記錄,返回一個int型數字,如  
count("username=:name",array(":name"=>$username));   


 二、$n=Post::model()->countBySql ($sql,$params); 
 該方法是根據SQL語句查詢一個集合有多少條記錄,返回一個int型數字,如 
countBySql("select *from admin whereusername=:name",array(':name'=>'admin'));


 三、$exists=Post::model()->exists($condition,$params); 
 該方法是根據一個條件查詢查詢獲得的數組有沒有數據,若是有數據返回一個true,不然沒有找到

 

 

 

        $sql = "select count(*) as count  from " . $this->tableName() . " where user_id = $userId";
        $query = $result->queryAll();
        $pageSize =10;
        $count = $query [0] ['count'];
        $pageCount = ceil($count / $pageSize);
        return $pageCount;



======================================== 4、添加的方法  
$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 (){

 

}


======================================== 5、修改的方法 

 


$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 ();

 

 

 


一、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;
}

 

 


 二、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傳入的值


 三、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 
 
======================================== 6、刪除的方法  一、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 "刪除失敗";  }   二、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 "刪除失敗";  

相關文章
相關標籤/搜索