Yii AR Model 查詢Yii AR很好很強(qiáng)大绞呈,但剛開始不知道怎么使用修改語言中文環(huán)境入口文件添加$application->language=isset(\Yii::$app->session['language'])?\Yii::$app->session['language']:'zh-CN';如果英文不錯乾胶,可以直接看原文地址http://www.yiiframework.com/doc/guide/1.1/en/database.ar
下面是我對AR的一些理解
對于一個Model Post 有如下的4中查詢方法客蹋,返回對象或者對象數(shù)組。
//find the first row satisfying the specified condition
復(fù)制代碼
$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);// find the first row using the specified SQL statement$post=Post::model()->findBySql($sql,$params);
復(fù)制代碼
假設(shè)我們查詢postID = 10
的數(shù)據(jù),怎么查詢呢悍缠,見下面
//find the row with postID=10
$post=Post::model()->find('postID=:postID', array(':postID'=>10));
條件$condition 就是我們sql里的where部分,那參數(shù)怎么辦呢,通過params傳遞秸仙,不過名字是加了":"的。
YII有個CDbCriteria類來構(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
你也可以寫成下面這樣
$post=Post::model()->find(array( 'select'=>'title', 'condition'=>'postID=:postID', 'params'=>array(':postID'=>10),));
findByAttributes 里的$attributes就是字段的名字.查詢title為abc怎么查詢呢?見下面Post::model()->findByAttributes(array('title'=>' abc'))
其它方法(轉(zhuǎn) http://hi.baidu.com/fegro/blog/item/8c8093ff886b8197b801a05f.html ):
1赌结、$admin=Admin::model()->findAll($condition,$params);
該方法是根據(jù)一個條件查詢一個集合捞蛋,如: findAll("username=:name",array(":name"=>$username));
2、$admin=Admin::model()->findAllByPk($postIDs,$condition,$params); findAllByPk($id,"name like ':name' and age=:age" ,array(':name'=>$name,'age'=>$age));
該方法是根據(jù)主鍵查詢一個集合柬姚,可以使用多個主鍵,如: findAllByPk(array(1,2));
3拟杉、$admin=Admin::model()->findAllByAttributes($attributes,$condition,$params);
該方法是根據(jù)條件查詢一個集合,可以是多個條件量承,把條件放到數(shù)組里面搬设,根據(jù)屬性來查詢,就想sql的where里的多個and如如: findAllByAttributes(array('username'=>'admin'));
SELECT * from one_table where username = 'admin' and password = '123456';
對應(yīng)的AR model查詢?nèi)缦翧dmin::model()->findAllByAttributes(array('username'=>'admin', 'password'=>'123456'));
4撕捍、$admin=Admin::model()->findAllBySql($sql,$params);
該方法是根據(jù)SQL語句查詢一個數(shù)組,如: findAllBySql("select *from admin where username=:name",array(':name'=>'admin'));
二拿穴、查詢對像的方法 1、$admin=Admin::model()->findByPk($postID,$condition,$params);
根據(jù)主鍵查詢出一個對象,如:findByPk(1);
2忧风、$row=Admin::model()->find($condition,$params);
根據(jù)一個條件查詢出一組數(shù)據(jù)默色,可能是多個,但是他只返回第一行數(shù)據(jù),如: find('username=:name',array(':name'=>'admin'));
3狮腿、$admin=Admin::model()->findByAttributes($attributes,$condition,$params);
該方法是根據(jù)條件查詢一組數(shù)據(jù)腿宰,可以是多個條件,把條件放到數(shù)組里面缘厢,他查詢的也是第一條數(shù)據(jù)吃度,如: findByAttributes(array('username'=>'admin'));
4、$admin=Admin::model()->findBySql($sql,$params);
該方法是根據(jù)SQL語句查詢一組數(shù)據(jù),他查詢的也是第一條數(shù)據(jù)昧绣,如: findBySql("select *from admin where username=:name",array(':name'=>'admin'));
5规肴、拼一個獲得SQL的方法,在根據(jù)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
三夜畴、查詢個數(shù)拖刃,判斷查詢是否有結(jié)果 1、$n=Post::model()->count($condition,$params);
該方法是根據(jù)一個條件查詢一個集合有多少條記錄贪绘,返回一個int型數(shù)字,如 count("username=:name",array(":name"=>$username));
2兑牡、$n=Post::model()->countBySql($sql,$params);
該方法是根據(jù)SQL語句查詢一個集合有多少條記錄,返回一個int型數(shù)字,如 countBySql("select *from admin where username=:name",array(':name'=>'admin'));
3税灌、$exists=Post::model()->exists($condition,$params);
該方法是根據(jù)一個條件查詢查詢得到的數(shù)組有沒有數(shù)據(jù)均函,如果有數(shù)據(jù)返回一個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代表?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ù)庫中的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 "刪除失敗"; }