Yii基于數(shù)組和對(duì)象的Model查詢技巧實(shí)例詳解
本文實(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)來(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)的屬性名和值,上面的例子可以重寫(xiě)為如下:
$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ì)有所幫助。
- Yii查詢生成器(Query Builder)用法實(shí)例教程
- Yii不依賴Model的表單生成器用法實(shí)例
- Yii框架關(guān)聯(lián)查詢with用法分析
- Yii2中使用join、joinwith多表關(guān)聯(lián)查詢
- Yii2增刪改查之查詢 where參數(shù)詳細(xì)介紹
- Yii多表聯(lián)合查詢操作詳解
- Yii框架參數(shù)化查詢中IN查詢只能查詢一個(gè)的解決方法
- YII2數(shù)據(jù)庫(kù)查詢實(shí)踐
- Yii2實(shí)現(xiàn)跨mysql數(shù)據(jù)庫(kù)關(guān)聯(lián)查詢排序功能代碼
- Yii中的relations數(shù)據(jù)關(guān)聯(lián)查詢及統(tǒng)計(jì)功能用法詳解
- YII2框架中查詢生成器Query()的使用方法示例
相關(guān)文章
PHP實(shí)現(xiàn)隨機(jī)生成水印圖片功能
這篇文章主要為大家詳細(xì)介紹了PHP生成隨機(jī)水印圖片的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03使用PHP?MySQL實(shí)現(xiàn)數(shù)據(jù)量小的內(nèi)容推薦方法
這篇文章主要為大家介紹了使用PHP?MySQL實(shí)現(xiàn)數(shù)據(jù)量小的內(nèi)容推薦方法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07純真IP數(shù)據(jù)庫(kù)的應(yīng)用 IP地址轉(zhuǎn)化成十進(jìn)制
由于純真數(shù)據(jù)庫(kù)中的IP數(shù)據(jù)是和普通IP不同的,所以要轉(zhuǎn)化后才能比對(duì)2009-06-06- 很多朋友不清楚php中sql查詢語(yǔ)句的id=%d的意思,今天小編通過(guò)本文給大家詳細(xì)介紹下PHP中SQL查詢語(yǔ)句的id=%d解釋?zhuān)枰呐笥褏⒖枷掳?/div> 2016-12-12
laravel 實(shí)現(xiàn)劃分admin和home 模塊分組
今天小編就為大家分享一篇laravel 實(shí)現(xiàn)劃分admin和home 模塊分組,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10PHP實(shí)現(xiàn)采集程序原理和簡(jiǎn)單示例代碼
PHP實(shí)現(xiàn)采集程序原理和簡(jiǎn)單示例代碼...2007-03-03PHP中__set()實(shí)例用法和基礎(chǔ)講解
在本篇文章里小編給大家整理了關(guān)于HP中__set()實(shí)例用法和基礎(chǔ)講解,對(duì)此有需要的朋友們可以學(xué)習(xí)參考下。2019-07-07windows系統(tǒng)php環(huán)境安裝swoole具體步驟
這篇文章主要介紹了windows系統(tǒng)php環(huán)境安裝swoole具體步驟,swoole目前是比較熱門(mén)的一個(gè)擴(kuò)展插件,有需要的同學(xué)可以學(xué)習(xí)下2021-03-03PHP setTime 設(shè)置當(dāng)前時(shí)間的代碼
在用JAVA中有個(gè) Calendar 可設(shè)置當(dāng)前時(shí)間,在PHP中找了半天,終于給我找到了這個(gè)函數(shù),嘎嘎2012-08-08最新評(píng)論