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