解析yii数据库的增删查改
程序员文章站
2022-12-28 09:08:37
1. 存取数据库方法存储第一种存表时候用到例子:复制代码 代码如下:$post=new post;$post->title='samplepost';$post-&g...
1. 存取数据库方法
存储第一种
存表时候用到
例子:
复制代码 代码如下:
$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();
注当一个表存储4次的时候,需要创建4个handle new4次
存储第二种
存储后我们需要找到这条记录的流水id 这样做 $profile = new profile;$profile->id;
存储第三种
用于更加安全的方法,来绑定变量类型 这样可以在同一个表中存储两个记录
复制代码 代码如下:
$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都可以用这个方法
$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)
{
更新成功后。。。
}
第四种更新和存储应用同一个handle 流程:
先查询记录是否存在,若存在就更新,不存在就新创建
注:1.第一次查询的变量,要跟save()前的变量一致。2.存储时候需要再次 new一下库对象
复制代码 代码如下:
$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();
}
查询
注:当项目没查找到整个对象会为空需要这样判定
复制代码 代码如下:
if($rows !== null) 当对象不为空
{
returntrue;
}else{
returnfalse;
}
select
读表时候用到
例子:
第一种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));
第三种当没有conditions时候,不用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));
此时回传的是一个对象
第四种 添加其他条件
http://www.yiiframework.com/doc/api/cdbcriteria#limit-detail
$criteria = newcdbcriteria;
$criteria->select='newtime';//选择只显示哪几个字段要与库中名字相同,但是不能count(newtime) as name这样写
$criteria->join = 'left joinpost on post.id=date.id';//1.先要在relation函数中增加与post表的关系语句2.date::model()->with('post')->findall($criteria)
$criteria->group ='newtime';
$criteria->limit = 2; //都是从0开始,选取几个
$criteria->offset = 2;// 从哪个偏移量开始
print_r(date::model()->findall($criteria));
得到行数目或者其他数目 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()函数合用,这样可以联合查询
$criteria=newcdbcriteria;
$criteria->condition='field.step_id=1';
$this->_post=field::model()->with('step')->findall($criteria);
这样出来的数组里面包含step表中的值,且这个值的条件为step.id=field.step_id
public functionrelations()
{
return array(
'step'=>array(self::belongs_to,'step', 'step_id'),
);
}
下一篇: oldboy s21day06