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
);
Yii 框架里数据库操作详解的更多相关文章
- Yii 框架里数据库操作详解-[增加、查询、更新、删除的方法 'AR模式']
public function getMinLimit () { $sql = "..."; $result = yii::app()->db-& ...
- windows phone 8.1开发SQlite数据库操作详解
原文出自:http://www.bcmeng.com/windows-phone-sqlite1/ 本文小梦将和大家分享WP8.1中SQlite数据库的基本操作:(最后有整个示例的源码)(希望能通过本 ...
- JMeter数据库操作详解
Jmeter提供了JDBC连接的插件,通过执行SQL语句的java API,实现对数据库的访问和查询,同时可以操作一次向数据库插入上百条上千条数据. 一.安装驱动包 将需要连接JDBC的jar包放入j ...
- android 数据库操作详解
请看郭大神的八篇专栏,包含sql语句 android封装的databasehelper 和郭大神自己的LitePal 三种使用详解 http://blog.csdn.net/column/deta ...
- yii2 数据库操作详解(转载)
开始使用数据库首先需要配置数据库连接组件,通过添加 db 组件到应用配置实现("基础的" Web 应用是 config/web.php),DSN( Data Source Name ...
- JDBC连接mysql数据库操作详解
1.什么是JDBC JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Jav ...
- django 数据库操作详解
Django配置使用mysql数据库 修改 settings.py 中的 DATABASES 注意:django框架不会自动帮我们生成mysql数据库,所以我们需要自己去创建. DATABASES ...
- Django框架 之 ORM查询操作详解
Django框架 之 ORM查询操作详解 浏览目录 一般操作 ForeignKey操作 ManyToManyField 聚合查询 分组查询 F查询和Q查询 事务 Django终端打印SQL语句 在Py ...
- SAE上传web应用(包括使用数据库)教程详解及问题解惑
转自:http://blog.csdn.net/baiyuliang2013/article/details/24725995 SAE上传web应用(包括使用数据库)教程详解及问题解惑: 最近由于工作 ...
随机推荐
- C#调用Exe文件的方法及如何判断程序调用的exe已结束
很简单的代码就可以实现C#调用EXE文件,如下: 引入using System.Diagnostics; 调用代码: Process.Start(exe文件名); 或直接 System.Diagnos ...
- Safecracker(搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=1015 / 题意; 从所给的一串字符串中选出5个字母假如是(A B C D E)使得A-B^2+C^3-D^4+E ...
- java中字节流和字符流的区别
流分类: 1.Java的字节流 InputStream是所有字节输入流的祖先,而OutputStream是所有字节输出流的祖先.2.Java的字符流 Reader是所有读取字符串输入流的祖先,而 ...
- Servlet的一些细节(2)
1. Servlet的创建时间 Servlet是不能单独运行,调用它的叫做Servlet引擎,或者叫做web服务器 针对客户端的多长Servlet请求,通常情况下,服务器只会创建一个Servlet实例 ...
- 菜鸟学习SSH(一)——Struts实现简单登录(附源码)
从今天开始,一起跟各位聊聊java的三大框架——SSH.先从Struts开始说起,Struts对MVC进行了很好的封装,使用Struts的目的是为了帮助我们减少在运用MVC设计模型来开发Web应用的时 ...
- web容器线程数和程序中线程阻塞导致 请求超时
问题描述: web项目启动之后.调用dubbo的远程服务. 但是有个基础服务报错. 当并发访问用户量上来之后. dubbo服务的报错返回 比正常服务慢 不能正常消费服务 清理服务线程. 也就是dubb ...
- 常用的Linux操作二
1.sudo 说明:以系统管理者的身份执行指令,也就是说,经由 sudo 所执行的指令就好像是 root 亲自执行 . 2.who 说明 : 显示系统中有那些使用者正在上面,显示的资料包含 ...
- Swift开发之 使用系统的TabbarController
使用系统的tabbar解决图片的问题. 用sb使用系统的tabbar,弄好后发现图片始终是蓝色的,并非原来的图片,后经上网查资料,问问同事,有了以下的这些代码,做个标记. AppDelegate中 f ...
- [PWA] 13. New db and object store
Create a db: import idb from 'idb'; var dbPromise = idb.open('test-db', 2, function (upgradeDb) { sw ...
- 高效实现 std::string split() API
Qt下一个 QString 实现split()性能.和std::string未实现它的.STL也未实现.只有自己可以写一. #include <string> #include <v ...