YII1 增、刪、改、查資料庫操作

OldBoy~發表於2018-12-07

 

//注意插入的時候 是用的new Post
$objectPost = new Post;      
$objectPost->username = $username;
$objectPost->password = $password;
//或許
$objectPost->attributes = $arrNewData;
if($objectPost->save()){
    $intPostId= $objectPost->primaryKey; //生成主鍵id
    echo "新增成功";
}else{
    echo "新增失敗";
    //在除錯的狀態下可以使用$objectPost->getErrors()來列印資料模型錯誤資訊 
}

刪 

根據條件刪除集合 deleteAll($condition='',$params=array());

$condition = 'username=:name and password=:pass';
$params = array(':name'=>'www.aipanshi.com',':pass'=>'aipanshi.com');
//刪除postid為這些的資料
$boolean = Post::model()->deleteAll('postid in("1,2,3")');
if($boolean > 0){
    echo "刪除成功";
}else{
    echo "刪除失敗";
}
 ```

根據主鍵刪除一個集合 deleteByPk($pk,$condition='',$params=array())

$boolean = Post::model()->deleteByPk(1);
$boolean = Post::model()->deleteByPk(array(1,2),'username=:name',array(':name'=>'aipanshi.com'));
if($boolean > 0){
    echo "刪除成功";
}else{
    echo "刪除失敗";
}

根據條件 updateAll($attributes,$condition='',$params=array())

$attributes = array('username'=>'www.aipanshi.com','postsort'=>'99');
$condition = 'status=:s';
$params = array(':s'=>2);
$boolean = Post::model()->updateAll($attributes,$condition,$params);
if($boolean > 0){
    echo "修改成功";
}else{
    echo "修改失敗";
}

根據主鍵 updateByPk($pk,$attributes,$condition='',$params=array())

/**
 * $pk主鍵 也可以是一個陣列集合
 * $attributes是要修改的欄位的集合
 * $condition條件
 * $params傳入的值
 */ 
$attributes = array('username'=>'www.aipanshi.com','password'=>'www.aipanshi.com');
$count =Post::model()->updateByPk(1,$attributes);
$attributes2 = array('username'=>'www.aipanshi.com1','password'=>'www.aipanshi.com1');
$condition2 = 'username=:name';
$params2 = array(':name'=>'www.aipanshi.com');
$boolean =Post::model()->updateByPk(array(1,2),$attributes2,$condition2,$params2);
if($boolean > 0){
    echo "修改成功";
}else{
    echo "修改失敗";
}

自增和自減 updateCounters($counters,$condition='',$params=array());

//資料庫中的post表根據條件username='www.aipanshi.com',查詢出的所有結果status欄位都自加1
$counters = array('status'=>1);//-1自減
$condition = 'username=:name';
$params = array(':name'=>'www.aipanshi.com');
$boolean=Post::model()->updateCounters($counters,$condition,$params);
if($boolean > 0){
    echo "修改成功";
}else{
    echo "修改失敗";
}

查詢指定欄位

$res = KnowledgeAuthList::model()->find([
            'select' => ['id'],
            'condition' => 'auth_name=:auth_name',
            'params' => [':auth_name'=>'基礎']
        ]);

根據條件查詢一個集合 findAll($condition='',$params=array())

$res=Post::model()->findAll("username=:name",array(":name"=>$username));
$res=Post::model()->findAll("username=:name and status=:s",array(":name"=>'jack',':s'=>1));
$info = Post::model()->findAll("status = '1' ORDER BY postid DESC limit 10 ");
 ```

根據主鍵查詢一個集合,可以使用多個主鍵 findAllByPk($pk,$condition='',$params=array())

$condition = "username like :name and status=:s";
$res=Post::model()->findAllByPk($ids,$condition,array(':name'=>$name,':s'=>1));
$res=Post::model()->findAllByPk(array(1,2));

根據陣列條件查詢一個集合 findAllByAttributes($attributes,$condition='',$params=array())

$res=Post::model()->findAllByAttributes($attributes,$condition,$params);
$res=Post::model()->findAllByAttributes(array('username'=>'www.aipanshi.com'));

根據SQL語句查詢一個陣列 findAllBySql($sql,$params=array())

$res=Post::model()->findAllBySql($sql,$params);
$sql = "select * from tbl_post where username like :name";
$res=Post::model()->findAllBySql($sql,array(':name'=>'%Jack%'));

根據主鍵查詢出一個物件 findByPk($pk,$condition='',$params=array())

$res=Post::model()->findByPk($postID,$condition,$params);
$res=Post::model()->findByPk(1);

根據條件查詢,他只返回第一行資料find($condition='',$params=array())

$res=Post::model()->find('username=:name',array(':name'=>'www.aipanshi.com'));

根據陣列條件查詢,查詢的也是第一條資料findByAttributes($attributes,$condition='',$params=array())

$res=Post::model()->findByAttributes(array('username'=>'www.aipanshi.com'));

根據SQL語句查詢,查詢的也是第一條資料 findBySql($sql,$params=array())

$sql = "select * from objectResult where username=:name";
$res=Post::model()->findBySql($sql,array(':name'=>'www.aipanshi.com'));

通過CDbCriteria類find查詢出一個物件

$c=new CDbCriteria; 
$c->select='username'; // 限制顯示哪些欄位 
$c->condition='username=:username';     //一個查詢條件用aCondition.多條件用addCondition
$c->params=array(":username=>'www.aipanshi.com'");
$c->order = "postsort DESC";
$c->limit = "3";
$post=Post::model()->find($c);

多條件查詢的語句

$c = new CDbCriteria;     
$c->addCondition("postid=1"); //等同於 where postid = 1 
$c->addInCondition('postid', array(1,2,3,4,5)); //等同於 where postid IN (1,2,3,4,5,); 
$c->addNotInCondition('postid', array(1,2,3,4,5));//等同於 NOT IN (1,2,3,4,5,)
$c->addCondition('postid=1','OR');//等同於 OR而非AND 
$c->addSearchCondition('username', 'www.aipanshi.com');//等同於 where name like '%www.aipanshi.com%' 
$c->addBetweenCondition('postid', 1, 4);// 等同於 between 1 and 4
$c->compare('postid', 1);    //根據你的引數自動處理成addCondition或者addInCondition.
$c->compare('postid', array(1,2,3));   //陣列就會呼叫addInCondition 
$c->select = 'postid,parentid,name'; //限制顯示哪些欄位 
$c->join = 'xxx'; //連線表 
$c->with = 'xxx'; //呼叫relations  
$c->limit = 10;    //取1條資料,如果小於0,則不作處理 
$c->offset = 1;   //兩條合併起來,則表示 limit 10 offset 1,或者代表了。limit 1,10 
$c->order = 'xxx DESC,XXX ASC' ;//排序條件 
$c->group = 'group 條件'; 
$c->having = 'having 條件 '; 
$c->distinct = FALSE; //是否唯一查詢

根據一個條件查詢一個集合有多少條記錄,返回一個int型數字count($condition='',$params=array())

$intCount=Post::model()->count("username=:name",array(":name"=>'aipanshi.com'));

根據SQL語句查詢一個集合有多少條記錄,返回int型 countBySql($sql,$params=array())

$sql = "select * from objectResult where username=:name";
$intCount=Post::model()->countBySql($sql,array(':name'=>'aipanshi.com'));

根據條件查詢有無資料返回一個boolean exists($condition='',$params=array())

$boolExist=Post::model()->exists("name=:name",array(":name"=>'aipanshi.com'));

查詢出複合條件的使用者陣列,指定了index以後,結果陣列$users已經是用id做為主鍵的了

$posts = Post::model()->findAll(array('condition'=>'postsort > 18', 'index'=> 'id'));

 

摘自:http://www.aipanshi.com/post/type.html?name=yii

 

相關文章