yii的一些方法的解析和blog的详细解析
1. 存取数据库方法
存储第一种(SAVE )
存表时候用到
例子:
$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次
存储第二种(存储)
存储后我们需要找到这条记录的流水id 这样做 $profile = new profile; $profile->id;
存储第三种 用于更加安全的方法,来绑定变量类型 这样可以在同一个表中存储两个记录
$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) {
//更新成功后。。。
}
第四种更新和存储应用同一个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();
}
2、查询数据
注:当项目没查找到整个对象会为空需要这样判定
当对象不为空
if($rows !== null) {
return true;
}else{
return false;
}
SELECT
读表时候用到
第一种find()
// find the first 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 primary key
$post=Post::model()->findByPk($postID,$condition,$params);
// find the row with the specified attribute values
$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));
此时回传的是一个对象
第四种 添加其他条件
http://www.yiiframework.com/doc/api/CDbCriteria#limit-detail
$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);
3、更新(UPDATE)
例子:
$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);
4、删除(DELETE)
例子:
$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);
5、比较(COMPARE)
目前可以取出的
1.$allquestion=field::model()->findAllBySql("select label 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=new CDbCriteria;
$criteria->condition='field.step_id=1';
$this->_post=field::model()->with('step')->findAll($criteria);
这样出来的数组里面包含step表中的值,且这个值的条件为 step.id=field.step_id
public function relations() {
return array(
'step'=>array(self::BELONGS_TO, 'step', 'step_id'),
);
}
yii的一些方法的解析和blog的详细解析的更多相关文章
- spring源码深度解析— IOC 之 默认标签解析(上)
概述 接前两篇文章 spring源码深度解析—Spring的整体架构和环境搭建 和 spring源码深度解析— IOC 之 容器的基本实现 本文主要研究Spring标签的解析,Spring的标签 ...
- YII 伪静态 IIS7 方法 web.config
YII 伪静态 IIS7 方法 web.config <?xml version="1.0" encoding="UTF-8"?> <conf ...
- spring mvc: 参数方法名称解析器(用参数来解析控制器下的方法)MultiActionController/ParameterMethodNameResolver/ControllerClassNameHandlerMapping
spring mvc: 参数方法名称解析器(用参数来解析控制器下的方法)MultiActionController/ParameterMethodNameResolver/ControllerClas ...
- 转:二十一、详细解析Java中抽象类和接口的区别
转:二十一.详细解析Java中抽象类和接口的区别 http://blog.csdn.net/liujun13579/article/details/7737670 在Java语言中, abstract ...
- android Json解析详解(详细代码)
JSON的定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据 ...
- 小白详细解析C#反射特性实例
套用MSDN上对于反射的定义:反射提供了封装程序集.模块和类型的对象(Type 类型).可以使用反射动态创建类型的实例,将类型绑定到现有对象,或从现有对象获取类型并调用其方法或访问其字段和属性.如果代 ...
- (转)libcurl库使用方法,好长,好详细。
一.ibcurl作为是一个多协议的便于客户端使用的URL传输库,基于C语言,提供C语言的API接口,支持DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP ...
- skb详细解析【转】
skb详细解析[转] 摘自:http://blog.chinaunix.net/uid-30035229-id-4883992.html 在自己的模块发送函数中,需要对skb进行重新构造 ...
- 【详细解析】MySQL索引详解( 索引概念、6大索引类型、key 和 index 的区别、其他索引方式)
[详细解析]MySQL索引详解( 索引概念.6大索引类型.key 和 index 的区别.其他索引方式) MySQL索引的概念: 索引是一种特殊的文件(InnoDB数据表上的索引是表空间的一个组成部分 ...
随机推荐
- html导出图片
有一个神奇的库:html2canvas. 这个库简洁优美,使用方便. 下面先看一个小demo,它需要用到一张图片:haha.jpg. <html> <head> <scr ...
- jquery.masonry瀑布流插件的4个使用步骤
1.分别加载jquery插件与jquery.masonry插件两个文件 下载jquery插件:http://jquery.com也可以引用google的cdn外部jquery核心库JS文件下载jque ...
- mac下安装mysql 1820 重置默认密码
mac安装mysql时会给出一个临时密码 记录下来 2018-03-17T02:14:10.809431Z 1 [Note] A temporary password is generated for ...
- select函数的并发限制和 poll 函数应用举例
一.用select实现的并发服务器,能达到的并发数,受两方面限制 1.一个进程能打开的最大文件描述符限制.这可以通过调整内核参数.可以通过ulimit -n来调整或者使用setrlimit函数设置, ...
- WinForm开发,窗体显示和窗体传值相关知识总结
主窗体中代码: public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void b ...
- window 环境 Composer 安装 thinkphp5
参考链接:https://www.kancloud.cn/thinkphp/thinkphp5_quickstart/478269 在 Windows 中,你需要下载并运行 Composer-Setu ...
- ubuntu14.4开启ftp服务
1 更新源列表 打开"终端窗口",输入"sudo apt-get update"-->回车-->"输入当前登录用户的管理员密码" ...
- 为anaconda的jupyter notebook设置初始化目录
在使用jupyter进行编程时,初始化目录可能不是自己想要的目录,那么下面讲解修改成自己想要的目录. 1) 在命令行中输入jupyter notebook --generate-config,会产生一 ...
- [hihoCoder] #1158 : 质数相关
时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 两个数a和 b (a<b)被称为质数相关,是指a × p = b,这里p是一个质数.一个集合S被称为质数相关,是指S中 ...
- CDH5.2+CM5.2+impala2+Spark1.1 集群搭建基础环境准备
測试集群简单介绍:一共同拥有4台机器:10.10.244.136.10.10.244.137.10.10.244.138.10.10.244.139. 10.10.244.136是管理节点.另外3台是 ...