《PHP實戰:Yii基于數組和對象的Model查詢技巧實例詳解》要點:
本文介紹了PHP實戰:Yii基于數組和對象的Model查詢技巧實例詳解,希望對您有用。如果有疑問,可以聯系我們。
相關主題:YII框架
PHP學習本文實例講述了Yii基于數組和對象的Model查詢技巧.分享給大家供大家參考,具體如下:
PHP學習對于一個Model Post 有如下的4中查詢辦法,返回對象或者對象數組.
PHP學習
//查找滿足指定條件的結果中的第一行 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);
PHP學習如果find 辦法找到了一個滿足查詢條件的行,它將返回一個Post 實例,實例的屬性含有數據表行中相應列的值.然后我們就可以像讀取普通對象的屬性那樣讀取載入的值,例如echo $post->title;.如果使用給定的查詢條件在數據庫中沒有找到任何東西, find 辦法將返回null.
PHP學習調用find 時,我們使用$condition 和$params 指定查詢條件.此處$condition 可以是SQL 語句中的WHERE 字符串,$params 則是一個參數數組,其中的值應綁定到$condation 中的占位符.例如:假設我們查詢postID = 10的數據
PHP學習
// find the row with postID=10
$post=Post::model()->find('postID=:postID', array(':postID'=>10));
PHP學習條件$condition 就是我們sql里的where部分,那參數怎么辦呢,通過params傳遞,不過名字是加了":"的.
PHP學習YII有個CDbCriteria類來構造查詢,如果我們查詢postId為10的title,CdbCriteria是這樣構造的
PHP學習
$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
PHP學習一種替代CDbCriteria 的辦法是給find 辦法傳遞一個數組.數組的鍵和值各自對應標準( criterion)的屬性名和值,上面的例子可以重寫為如下:
PHP學習
$post=Post::model()->find(array(
'select'=>'title',
'condition'=>'postID=:postID',
'params'=>array(':postID'=>10),
));
PHP學習當然也適用于findAll()
PHP學習
self::$_items[$type]=array();
$models=self::model()->findAll(array(
'condition'=>'type=:type',
'params'=>array(':type'=>$type),
'order'=>'position',
));
PHP學習當一個查詢條件是關于按指定的值匹配幾個列時,我們可以使用findByAttributes().我們使$attributes 參數是一個以列名做索引的值的數組.
findByAttributes 里的$attributes就是字段的名字.查詢title為abc怎么查詢呢?見下面
PHP學習其它辦法:
PHP學習1、$admin=Admin::model()->findAll($condition,$params);
PHP學習該辦法是根據一個條件查詢一個集合,如:
PHP學習該辦法是根據條件查詢一個集合,可以是多個條件,把條件放到數組里面,如:
PHP學習該辦法是根據SQL語句查詢一個數組,如:
PHP學習二、查詢對像的辦法
PHP學習1、$admin=Admin::model()->findByPk($postID,$condition,$params);
PHP學習根據主鍵查詢出一個對象,如:
PHP學習根據一個條件查詢出一組數據,可能是多個,但是他只返回第一行數據,如:
PHP學習該辦法是根據條件查詢一組數據,可以是多個條件,把條件放到數組里面,他查詢的也是第一條數據,如:
PHP學習該辦法是根據SQL語句查詢一組數據,他查詢的也是第一條數據,如:
PHP學習
$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
PHP學習三、查詢個數,判斷查詢是否有結果
PHP學習1、$n=Post::model()->count($condition,$params);
PHP學習該辦法是根據一個條件查詢一個集合有多少條記錄,返回一個int型數字,如
PHP學習該辦法是根據SQL語句查詢一個集合有多少條記錄,返回一個int型數字,如
PHP學習四、添加的辦法
PHP學習
$admin=new Admin;
$admin->username=$username;
$admin->password=$password;
if($admin->save()>0){
echo "添加成功";
}else{
echo "添加失敗";
}
PHP學習五、修改的辦法
PHP學習1、Post::model()->updateAll($attributes,$condition,$params);
PHP學習
$count = Admin::model()->updateAll(array('username'=>'11111','password'=>'11111'),'password=:pass',array(':pass'=>'1111a1'));
if($count>0){
echo "修改成功";
}else{
echo "修改失敗";
}
PHP學習2、Post::model()->updateByPk($pk,$attributes,$condition,$params);
PHP學習
$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 "修改失敗";
}
PHP學習$pk代表主鍵,可以是一個也可以是一個集合,$attributes代表是要修改的字段的集合,$condition代表條件,$params傳入的值
PHP學習3、Post::model()->updateCounters($counters,$condition,$params);
PHP學習
$count =Admin::model()->updateCounters(array('status'=>1),'username=:name',array(':name'=>'admin'));
if($count>0){
echo "修改成功";
}else{
echo "修改失敗";
}
PHP學習array('status'=>1)代表數據庫中的admin表根據條件username='admin',查詢出的所有結果status字段都自加1
PHP學習六、刪除的辦法
PHP學習1、Post::model()->deleteAll($condition,$params);
PHP學習
$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 "刪除失敗";
}
PHP學習2、Post::model()->deleteByPk($pk,$condition,$params);
PHP學習
$count = Admin::model()->deleteByPk(1);
$count = Admin::model()->deleteByPk(array(1,2),'username=:name',array(':name'=>'admin'));
if($count>0){
echo "刪除成功";
}else{
echo "刪除失敗";
}
PHP學習希望本文所述對大家基于Yii框架的PHP程序設計有所贊助.
歡迎參與《PHP實戰:Yii基于數組和對象的Model查詢技巧實例詳解》討論,分享您的想法,維易PHP學院為您提供專業教程。
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/7978.html