解析yii数据库的增删查改


Posted in PHP onJune 20, 2013

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'),
);
}
PHP 相关文章推荐
PHP校验ISBN码的函数代码
Jan 17 PHP
比较时间段一与时间段二是否有交集的php函数
May 31 PHP
php中计算中文字符串长度、截取中文字符串的函数代码
Aug 09 PHP
php页面缓存ob系列函数介绍
Oct 18 PHP
探讨:如何使用PHP实现计算两个日期间隔的年、月、周、日数
Jun 13 PHP
PHP采用get获取url汉字出现乱码的解决方法
Nov 13 PHP
php字符串过滤与替换小结
Jan 26 PHP
CI框架的安全性分析
May 18 PHP
PHP读书笔记_运算符详解
Jul 01 PHP
Yii2实现让关联字段支持搜索功能的方法
Aug 10 PHP
Laravel框架实现的使用smtp发送邮件功能示例
Mar 12 PHP
Laravel5.1 框架Request请求操作常见用法实例分析
Jan 04 PHP
在yii中新增一个用户验证的方法详解
Jun 20 #PHP
浅析Yii中使用RBAC的完全指南(用户角色权限控制)
Jun 20 #PHP
php中0,null,empty,空,false,字符串关系的详细介绍
Jun 20 #PHP
解析PHP中数组元素升序、降序以及重新排序的函数
Jun 20 #PHP
解析php中的fopen()函数用打开文件模式说明
Jun 20 #PHP
深入解析PHP内存管理之谁动了我的内存
Jun 20 #PHP
解析php中die(),exit(),return的区别
Jun 20 #PHP
You might like
PHP入门教程之表单与验证实例详解
2016/09/11 PHP
php版微信公众平台之微信网页登陆授权示例
2016/09/23 PHP
PHP解耦的三重境界(浅谈服务容器)
2017/03/13 PHP
Yii2.0多文件上传实例说明
2017/07/24 PHP
php 比较获取两个数组相同和不同元素的例子(交集和差集)
2019/10/18 PHP
实现JavaScript中继承的三种方式
2009/10/16 Javascript
JScript 脚本实现文件下载 一般用于下载木马
2009/10/29 Javascript
JavaScript 在网页上单击鼠标的地方显示层及关闭层
2012/12/30 Javascript
jquery select动态加载选择(兼容各种浏览器)
2013/02/01 Javascript
整理的比较全的event对像在ie与firefox浏览器中的区别
2013/11/25 Javascript
jquery实现拖拽调整Div大小
2015/01/30 Javascript
浅谈jQuery.easyui的datebox格式化时间
2015/06/25 Javascript
基于jQuery实现网页打印功能
2015/12/01 Javascript
jQuery使用$.ajax进行即时验证实例详解
2015/12/11 Javascript
AngularJS 中的指令实践开发指南(一)
2016/03/20 Javascript
JavaScript数据绑定实现一个简单的 MVVM 库
2016/04/08 Javascript
ES6新特性四:变量的解构赋值实例
2017/04/21 Javascript
jQuery中图片展示插件highslide.js的简单dom
2018/04/22 jQuery
javascript面向对象创建对象的方式小结
2019/07/29 Javascript
vscode 配置vue+vetur+eslint+prettier自动格式化功能
2020/03/23 Javascript
Python实现Mysql数据库连接池实例详解
2017/04/11 Python
Python下实现的RSA加密/解密及签名/验证功能示例
2017/07/17 Python
python机器学习之决策树分类详解
2017/12/20 Python
python对list中的每个元素进行某种操作的方法
2018/06/29 Python
加大码胸罩、内裤和服装:Just My Size
2019/03/21 全球购物
会计电算化专业应届大学生求职信
2013/10/22 职场文书
会计电算化专业毕业生求职信范文
2013/12/10 职场文书
春节联欢晚会主持词范文
2014/03/24 职场文书
5.12护士节演讲稿
2014/04/30 职场文书
2014年秋季开学寄语
2014/08/02 职场文书
个人买房协议书范本
2014/10/06 职场文书
道歉信怎么写
2015/05/12 职场文书
大学运动会加油稿
2015/07/22 职场文书
2016年“世界气象日”广播稿
2015/12/17 职场文书
教师外出学习心得体会
2016/01/18 职场文书
《岳阳楼记》原文、译文赏析
2019/09/10 职场文书