本文實例講述了Yii基于數組和對象的Model查詢技巧。分享給大家供大家參考,具體如下:
對于一個Model Post 有如下的4中查詢方法,返回對象或者對象數組。
//查找滿足指定條件的結果中的第一行 find the first row satisfying the specified condition$post=Post::model()->find($condition,$params);//查找具有指定主鍵值的那一行 find the row with the specified primary key$post=Post::model()->findByPk($postID,$condition,$params);//查找具有指定屬性值的行 find the row with the specified attribute values$post=Post::model()->findByAttributes($attributes,$condition,$params);//未找到返回null//通過指定的SQL 語句查找結果中的第一行 find the first row using the specified SQL statement$post=Post::model()->findBySql($sql,$params);
如果find 方法找到了一個滿足查詢條件的行,它將返回一個Post 實例,實例的屬性含有數據表行中相應列的值。然后我們就可以像讀取普通對象的屬性那樣讀取載入的值,例如echo $post->title;。如果使用給定的查詢條件在數據庫中沒有找到任何東西, find 方法將返回null。
調用find 時,我們使用$condition 和$params 指定查詢條件。此處$condition 可以是SQL 語句中的WHERE 字符串,$params 則是一個參數數組,其中的值應綁定到$condation 中的占位符。例如:假設我們查詢postID = 10的數據
// find the row with postID=10$post=Post::model()->find('postID=:postID', array(':postID'=>10));
條件$condition 就是我們sql里的where部分,那參數怎么辦呢,通過params傳遞,不過名字是加了":"的。
YII有個CDbCriteria類來構造查詢,如果我們查詢postId為10的title,CdbCriteria是這樣構造的
$criteria=new CDbCriteria;$criteria->select='title'; // only select the 'title' column$criteria->condition='postID=:postID';$criteria->params=array(':postID'=>10);$post=Post::model()->find($criteria); // $params is not needed
一種替代CDbCriteria 的方法是給find 方法傳遞一個數組。數組的鍵和值各自對應標準( criterion)的屬性名和值,上面的例子可以重寫為如下:
$post=Post::model()->find(array( 'select'=>'title', 'condition'=>'postID=:postID', 'params'=>array(':postID'=>10),));
當然也適用于findAll()
self::$_items[$type]=array();$models=self::model()->findAll(array( 'condition'=>'type=:type', 'params'=>array(':type'=>$type), 'order'=>'position',));
當一個查詢條件是關于按指定的值匹配幾個列時,我們可以使用findByAttributes()。我們使$attributes 參數是一個以列名做索引的值的數組。
findByAttributes 里的$attributes就是字段的名字.查詢title為abc怎么查詢呢?見下面
其它方法:
1、$admin=Admin::model()->findAll($condition,$params);
該方法是根據一個條件查詢一個集合,如:
2、$admin=Admin::model()->findAllByPk($postIDs,$condition,$params);
findAllByPk($id,"name like ':name' and age=:age" ,array(':name'=>$name,'age'=>$age));
該方法是根據主鍵查詢一個集合,可以使用多個主鍵,如:
3、$admin=Admin::model()->findAllByAttributes($attributes,$condition,$params);
該方法是根據條件查詢一個集合,可以是多個條件,把條件放到數組里面,如:
4、$admin=Admin::model()->findAllBySql($sql,$params);
該方法是根據SQL語句查詢一個數組,如:
二、查詢對像的方法
1、$admin=Admin::model()->findByPk($postID,$condition,$params);
根據主鍵查詢出一個對象,如:
2、$row=Admin::model()->find($condition,$params);
根據一個條件查詢出一組數據,可能是多個,但是他只返回第一行數據,如:
3、$admin=Admin::model()->findByAttributes($attributes,$condition,$params);
該方法是根據條件查詢一組數據,可以是多個條件,把條件放到數組里面,他查詢的也是第一條數據,如:
4、$admin=Admin::model()->findBySql($sql,$params);
該方法是根據SQL語句查詢一組數據,他查詢的也是第一條數據,如:
5、拼一個獲得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 is not needed
三、查詢個數,判斷查詢是否有結果
1、$n=Post::model()->count($condition,$params);
該方法是根據一個條件查詢一個集合有多少條記錄,返回一個int型數字,如
2、$n=Post::model()->countBySql($sql,$params);
該方法是根據SQL語句查詢一個集合有多少條記錄,返回一個int型數字,如
3、$exists=Post::model()->exists($condition,$params);
該方法是根據一個條件查詢查詢得到的數組有沒有數據,如果有數據返回一個true,否則沒有找到
四、添加的方法
$admin=new Admin;$admin->username=$username;$admin->password=$password;if($admin->save()>0){ echo "添加成功";}else{ echo "添加失敗";}
五、修改的方法
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 "修改失敗";}
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=:name and password=: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 "刪除失敗";}
希望本文所述對大家基于Yii框架的PHP程序設計有所幫助。