解析yii數(shù)據(jù)庫的增刪查改
1. 存取數(shù)據(jù)庫方法
存儲(chǔ)第一種
存表時(shí)候用到
例子:
$post=new Post;
$post->title='samplepost';
$post->content='content for thesample post';
$post->createTime=time();/$post->createTime=newCDbexpression_r('NOW()');
$post->save();
$user_field_data= new user_field_data;
$user_field_data->flag=0;
$user_field_data->user_id=$profile->id;
$user_field_data->field_id=$_POST['emailhiden'];
$user_field_data->value1=$_POST['email'];
$user_field_data->save();
注當(dāng)一個(gè)表存儲(chǔ)4次的時(shí)候,需要?jiǎng)?chuàng)建4個(gè)handle new4次
存儲(chǔ)第二種
存儲(chǔ)后我們需要找到這條記錄的流水id 這樣做 $profile = new profile;$profile->id;
存儲(chǔ)第三種
用于更加安全的方法,來綁定變量類型 這樣可以在同一個(gè)表中存儲(chǔ)兩個(gè)記錄
$sql="insert intouser_field_data(user_id,field_id,flag,value1)values(:user_id,:field_id,:flag,:value1);";
$command=user_field_data::model()->dbConnection->createCommand($sql);
$command->bindParam(":user_id",$profile->id,PDO::PARAM_INT);
$command->bindParam(":field_id",$_POST['firstnamehiden'],PDO::PARAM_INT);
$command->bindParam(":flag",$tmpflag,PDO::PARAM_INT);
$command->bindParam(":value1",$_POST['firstname'],PDO::PARAM_STR);
$command->execute();
$command->bindParam(":user_id",$profile->id,PDO::PARAM_INT);
$command->bindParam(":field_id",$_POST['emailhiden'],PDO::PARAM_INT);
$command->bindParam(":flag",$tmpflag,PDO::PARAM_INT);
$command->bindParam(":value1",$_POST['email'],PDO::PARAM_STR);
$rowchange =$command->execute();
if( $rowchange != 0){ 修改成功 }//用來判斷
注:update delete都可以用這個(gè)方法
$sql="delete from profile whereid=:id";
$command=profile::model()->dbConnection->createCommand($sql);
$command->bindParam(":id",$userid,PDO::PARAM_INT);
$this->rowflag=$command->execute();
$sql="update profile setpass=:pass,role=:role where id=:id";
$command=profile::model()->dbConnection->createCommand($sql);
$command->bindParam(":pass",$password,PDO::PARAM_STR);
$command->bindParam(":role",$role,PDO::PARAM_INT);
$command->bindParam(":id",$userid,PDO::PARAM_INT);
$this->rowflag=$command->execute();
// 同理變更updateAll()模式
$sql="update user_field_data set flag =:flag where user_id= :user_id and field_id= :field_id ";
原始sql語句
$criteria = newCDbCriteria;
$criteria->condition ='user_id = :user_id and field_id= :field_id';
$criteria->params =array(':user_id' => $userid,':field_id'=> $fieldid);
$arrupdate = array('flag'=> $flag);
if(user_field_data::model()->updateAll($arrupdate,$criteria)!= 0)
{
更新成功后。。。
}
第四種更新和存儲(chǔ)應(yīng)用同一個(gè)handle 流程:
先查詢記錄是否存在,若存在就更新,不存在就新創(chuàng)建
注:1.第一次查詢的變量,要跟save()前的變量一致。2.存儲(chǔ)時(shí)候需要再次 new一下庫對(duì)象
$user_field_data =user_field_data::model()->findByAttributes(
$attributes = array('user_id'=>Yii::app()->user->user_id, 'field_id'=> $key));
if($user_field_data !== null)
{
$user_field_data->value1= $value;
$user_field_data->save();
}
else
{
$user_field_data= new user_field_data;
$user_field_data->user_id= Yii::app()->user->user_id;
$user_field_data->field_id= $key;
$user_field_data->value1= $value;
$user_field_data->save();
}
查詢
注:當(dāng)項(xiàng)目沒查找到整個(gè)對(duì)象會(huì)為空需要這樣判定
if($rows !== null) 當(dāng)對(duì)象不為空
{
returntrue;
}else{
returnfalse;
}
SELECT
讀表時(shí)候用到
例子:
第一種find()
// find thefirst row satisfying the specified condition
$post=Post::model()->find($condition,$params);
// find the row with postID=10
$post=Post::model()->find('postID=:postID',array(':postID'=>10));
同樣的語句,用另種方式表示
$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
第二種find()
$post=Post::model()->find(array(
'select'=>'title',
'condition'=>'postID=:postID',
'params'=>array(':postID'=>10),
));
// find the row with the specified primarykey
$post=Post::model()->findByPk($postID,$condition,$params);
// find the row with the specified attributevalues
$post=Post::model()->findByAttributes($attributes,$condition,$params);
示例:
第一種findByAttributes()
$checkuser= user_field_data::model()->findByAttributes(
array('user_id' =>Yii::app()->user->user_id, 'field_id'=> $fieldid));
第二種findByAttributes()
$checkuser =user_field_data::model()->findByAttributes(
$attributes = array('user_id'=>Yii::app()->user->user_id, 'field_id'=> $fieldid));
第三種當(dāng)沒有conditions時(shí)候,不用params
$user_field_data=user_field_data::model()->findAllByAttributes(
$attributes = array('user_id'=> ':user_id'),
$condition = "field_id in(:fields)",
$params = array(':user_id'=>Yii::app()->user->user_id, ':fields'=> "$rule->dep_fields"));
// find the first row using the specified SQLstatement
$post=Post::model()->findBySql($sql,$params);
例子
user_field_data::model()->findBySql("selectid from user_field_data where user_id = :user_id and field_id =:field_id ", array(':user_id' =>$userid,':field_id'=>$fieldid));
此時(shí)回傳的是一個(gè)對(duì)象
第四種 添加其他條件
http://www.yiiframework.com/doc/api/CDbCriteria#limit-detail
$criteria = newCDbCriteria;
$criteria->select='newtime';//選擇只顯示哪幾個(gè)字段要與庫中名字相同,但是不能COUNT(newtime) as name這樣寫
$criteria->join = 'LEFT JOINPost ON Post.id=Date.id';//1.先要在relation函數(shù)中增加與Post表的關(guān)系語句2.Date::model()->with('post')->findAll($criteria)
$criteria->group ='newtime';
$criteria->limit = 2; //都是從0開始,選取幾個(gè)
$criteria->offset = 2;// 從哪個(gè)偏移量開始
print_r(Date::model()->findAll($criteria));
得到行數(shù)目或者其他數(shù)目 count
// get the number of rows satisfying thespecified condition
$n=Post::model()->count($condition,$params);
// get the number of rows using the specifiedSQL statement
$n=Post::model()->countBySql($sql,$params);
// check if there is at least a row satisfyingthe specified condition
$exists=Post::model()->exists($condition,$params);
UPDATE
例子:
$post=Post::model()->findByPk(10);
$post->title='new posttitle';
$post->save(); // save thechange to database
// update the rows matching the specifiedcondition
Post::model()->updateAll($attributes,$condition,$params);
例子:或者參考上面例子
$c=new CDbCriteria;
$c->condition='something=1';
$c->limit=10;
$a=array('name'=>'NewName');
Post::model()->updateAll($a,$c);
// update the rows matching the specifiedcondition and primary key(s)
Post::model()->updateByPk($pk,$attributes,$condition,$params);
例子
$profile =profile::model()->updateByPk(
Yii::app()->user->user_id,
$attributes = array('pass' =>md5($_POST['password']), 'role' => 1));
// update counter columns in the rowssatisfying the specified conditions
Post::model()->updateCounters($counters,$condition,$params);
DELETE
例子:
$post=Post::model()->findByPk(10);// assuming there is a post whose ID is 10
$post->delete(); // delete therow from the database table
// delete the rows matching the specifiedcondition
Post::model()->deleteAll($condition,$params);
// delete the rows matching the specifiedcondition and primary key(s)
Post::model()->deleteByPk($pk,$condition,$params);
COMPARE
目前可以取出的
1.//$allquestion=field::model()->findAllBySql("selectlabel from field where step_id = :time1 ", array(':time1'=>1));
2. //$criteria=new CDbCriteria;
//$criteria->select='label,options';
//$criteria->condition='step_id=:postID';
//$criteria->params=array(':postID'=>1);
//$allquestion=field::model()->findAll($criteria);
//$allquestion=field::model()->find("",array("label"));
可以與在models文件夾中的 庫連接文件relations()函數(shù)合用,這樣可以聯(lián)合查詢
$criteria=newCDbCriteria;
$criteria->condition='field.step_id=1';
$this->_post=field::model()->with('step')->findAll($criteria);
這樣出來的數(shù)組里面包含step表中的值,且這個(gè)值的條件為step.id=field.step_id
public functionrelations()
{
return array(
'step'=>array(self::BELONGS_TO,'step', 'step_id'),
);
}
- Yii操作數(shù)據(jù)庫的3種方法
- PHP的Yii框架中使用數(shù)據(jù)庫的配置和SQL操作實(shí)例教程
- Yii2框架數(shù)據(jù)庫簡單的增刪改查語法小結(jié)
- Yii2.0高級(jí)框架數(shù)據(jù)庫增刪改查的一些操作
- Yii2——使用數(shù)據(jù)庫操作匯總(增刪查改、事務(wù))
- YII2數(shù)據(jù)庫查詢實(shí)踐
- Yii實(shí)現(xiàn)多數(shù)據(jù)庫主從讀寫分離的方法
- yii數(shù)據(jù)庫的查詢方法
- Yii2數(shù)據(jù)庫操作常用方法小結(jié)
- Yii操作數(shù)據(jù)庫實(shí)現(xiàn)動(dòng)態(tài)獲取表名的方法
- Yii 連接、修改 MySQL 數(shù)據(jù)庫及phpunit 測(cè)試連接
- Yii 框架使用數(shù)據(jù)庫(databases)的方法示例
相關(guān)文章
php switch語句多個(gè)值匹配同一代碼塊應(yīng)用示例
switch 語句一行接一行地執(zhí)行(實(shí)際上是語句接語句)。開始時(shí)沒有代碼被執(zhí)行,下面有個(gè)示例,需要的朋友可以參考下2014-07-07WampServer搭建php環(huán)境時(shí)遇到的問題匯總
這篇文章主要介紹了WampServer搭建php環(huán)境時(shí)遇到的常見問題的解決辦法匯總,以上所述就是本文的全部內(nèi)容了。2015-07-07PHP-FPM運(yùn)行狀態(tài)的實(shí)時(shí)查看及監(jiān)控詳解
php-fpm和nginx一樣內(nèi)建了一個(gè)狀態(tài)頁,對(duì)于想了解php-fpm的狀態(tài)以及監(jiān)控php-fpm非常有幫助。這篇文章就給大家詳細(xì)介紹了PHP-FPM運(yùn)行狀態(tài)的實(shí)時(shí)查看及監(jiān)控,有需要的朋友們可以參考學(xué)習(xí),感興趣的朋友們下面來一起看看吧。2016-11-11PHP刪除二維數(shù)組中相同元素及數(shù)組重復(fù)值的方法示例
這篇文章主要介紹了PHP刪除二維數(shù)組中相同元素及數(shù)組重復(fù)值的方法,涉及php針對(duì)數(shù)組的遍歷、判斷、比較等相關(guān)操作技巧,需要的朋友可以參考下2017-05-05