Yii增删改查操作
增: 1 第一种
$post=new Post;$post->title='sample post';$post->content='content for the sample post';$post->createTime=time();/$post->createTime=new CDbexpression_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次
2 第二种
存储后我们需要找到这条记录的流水id 这样做 $profile = new profile; $profile->id;
3 第三种
用于更加安全的方法,来绑定变量类型 这样可以在同一个表中存储两个记录
$sql="insert into user_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 where id=:id";$command=profile::model()->dbConnection->createCommand($sql);$command->bindParam(":id",$userid,PDO::PARAM_INT);$this->rowflag=$command->execute();$sql="update profile set pass=: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 = new CDbCriteria;$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){更新成功后。。。}
4 第四种
更新和存储应用同一个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();}删:
$post=Post::model()->findByPk(10); // assuming there is a post whose ID is 10 $post->delete(); // delete the row from the database table // delete the rows matching the specified condition Post::model()->deleteAll($condition,$params); // delete the rows matching the specified condition and primary key(s) Post::model()->deleteByPk($pk,$condition,$params);
改:
例子: $post=Post::model()->findByPk(10); $post->title=’new post title’; $post->save(); // save the change to database
// update the rows matching the specified condition 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 specified condition 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 rows satisfying the specified conditions Post::model()->updateCounters($counters,$condition,$params);
查: 注:当项目没查找到整个对象会为空需要这样判定
if($rows !== null) 当对象不为空{ return true;}else{ return false;} SELECT,读表时候用到 find() 第一种find()
// find the first row satisfying the specified condition$post=Post::model()->find($condition,$params);// 条件查询$post = Post::model()->find('post_id=:post_id AND status=:status',array( ':post_id'=>8, ':status'=>'active',)); 同样的语句,用另种方式表示
$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 primary key$post=Post::model()->findByPk($postID,$condition,$params);findByAttributes() $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 SQL statement $post=Post::model()->findBySql($sql,$params); 例子 user_field_data::model()->findBySql(“select id from user_field_data where user_id = :user_id and field_id = :field_id “, array(‘:user_id’ => $userid,’:field_id’=>$fieldid)); 此时回传的是一个对象
第四种 添加其他条件
$criteria = new CDbCriteria;$criteria->select ='newtime';//选择只显示哪几个字段要与库中名字相同,但是不能COUNT(newtime) as name这样写$criteria->join = 'LEFT JOIN Post 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 the specified condition$n=Post::model()->count($condition,$params);// get the number of rows using the specified SQL statement$n=Post::model()->countBySql($sql,$params);// check if there is at least a row satisfying the specified condition$exists=Post::model()->exists($condition,$params);YYii增删改查操作的更多相关文章
- mongoVUE的增删改查操作使用说明
mongoVUE的增删改查操作使用说明 一. 查询 1. 精确查询 1)右键点击集合名,再左键点击Find 或者直接点击工具栏上的Find 2)查询界面,包括四个区域 {Find}区,查询条件格式{& ...
- (转)SQLite数据库增删改查操作
原文:http://www.cnblogs.com/linjiqin/archive/2011/05/26/2059182.html SQLite数据库增删改查操作 一.使用嵌入式关系型SQLite数 ...
- 详谈easyui datagrid增删改查操作
转自:http://blog.csdn.net/abauch_d/article/details/7734395 前几天我把easyui dadtagrid的增删改查的实现代码贴了出来,发现访问量达到 ...
- PHP程序中使用PDO对象实现对数据库的增删改查操作的示例代码
PHP程序中使用PDO对象实现对数据库的增删改查操作(PHP+smarty) dbconn.php <?php //------------------------使用PDO方式连接数据库文件- ...
- 浅谈dataGridView使用,以及画面布局使用属性,对datagridview进行增删改查操作,以及委托使用技巧
通过几天的努力后,对datagridview使用作一些简要的介绍,该实例主要运用与通过对datagridview操作.对数据进行增删改查操作时,进行逻辑判断执行相关操作.简单的使用委托功能,实 ...
- 05_Elasticsearch 单模式下API的增删改查操作
05_Elasticsearch 单模式下API的增删改查操作 安装marvel 插件: zjtest7-redis:/usr/local/elasticsearch-2.3.4# bin/plugi ...
- JDBC连接数据库及增删改查操作
什么是JDBC?Java语言访问数据库的一种规范,是一套APIJDBC (Java Database Connectivity) API,即Java数据库编程接口,是一组标准的Java语言中的接口和类 ...
- Elasticsearch 单模式下API的增删改查操作
<pre name="code" class="html">Elasticsearch 单模式下API的增删改查操作 http://192.168. ...
- Android SQLite 数据库 增删改查操作
Android SQLite 数据库 增删改查操作 转载▼ 一.使用嵌入式关系型SQLite数据库存储数据 在Android平台上,集成了一个嵌入式关系型数据库--SQLite,SQLite3支持NU ...
随机推荐
- 登录不到phpmyadmin
或许出现以下错误情况:phpmyadmin:#1045 无法登录 MySQL 服务器.Access denied for user ‘root’@’localhost’ (using password ...
- Invitation Cards---poj1511(spfa)
题目链接:http://poj.org/problem?id=1511 有向图有n个点m条边,求点1到其他n-1个点的最短距离和+其他点到点1的最小距离和: 和poj3268一样,但是本题的数据范围较 ...
- UIBezierPath 画线
使用UIBezierPath类可以创建基于矢量的路径,这个类在UIKit中.此类是Core Graphics框架关于path的一个封装.使用此类可以定义简单的形状,如椭圆或者矩形,或者有多个直线和曲线 ...
- Java学习-040-级联删除目录中的文件、目录
之前在写应用模块,进行单元测试编码的时候,居然脑洞大开居然创建了一个 N 层的目录,到后来删除测试结果目录的时候,才发现删除不了了,提示目录过长无法删除.网上找了一些方法,也找了一些粉碎机,都没能达到 ...
- 通过ping确定网卡mtu
使用 ping 测试确定网卡最佳 MTU 的方法 MTU ( Maximum Transmission Unit ,最大传输单元)是指某一层协议上一次能最大传输的数据量.当一个数据包超过 MTU 数据 ...
- Android各个版本 版本号对应关系表
Platform Version API Level VERSION_CODE Notes Android 5.0 21 LOLLIPOP Platform Highlights Android 4. ...
- 网站提供的下载IE8很慢 由于Microsoft 联机服务暂时不可用,SmartScreen筛选器无法检查此网站。
在内网环境中,网站系统提供了一个下载功能,用ie8下载特别慢,一个20kb的文件,下载要10分钟,但是在其他环境中是很快的,试了半天,原来是:由于Microsoft 联机服务暂时不可用,SmartSc ...
- MVC项目实践,在三层架构下实现SportsStore-08,部署到IIS服务器
SportsStore是<精通ASP.NET MVC3框架(第三版)>中演示的MVC项目,在该项目中涵盖了MVC的众多方面,包括:使用DI容器.URL优化.导航.分页.购物车.订单.产品管 ...
- SWIFT 闭包的简单使用二
import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: ...
- swap文件
# dd if=/dev/zero of=/tmp/myswap bs=1M count=4096 # mkswap /tmp/myswap # swapon /tmp/myswap # vim /e ...