ActiveRecord类文档:http://www.yiiframework.com/doc/guide/1.1/en/database.ar

对于一个Model Post 有如下的4中查询方法,返回对象或者对象数组。

// find the first row satisfying the specified condition

// find the first row satisfying the specified condition
$post=Post::model()->find($condition,$params);
// 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);
// find the first row using the specified SQL statement
$post=Post::model()->findBySql($sql,$params);

假设我们查询postID = 10的数据,怎么查询呢,见下面

// find the row with postID=10

$post=Post::model()->find('postID=:postID', array(':postID'=>10));

条件$condition 就是我们sql里的where部分,那参数怎么办呢,通过params传递,不过名字是加了":"的。

YII有个CDbCriteria类来构造查询,如果我们查询postId为10的title,CdbCriteria是这样构造的

$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

你也可以写成下面这样

$post=Post::model()->find(array(
    'select'=>'title',
    'condition'=>'postID=:postID',
    'params'=>array(':postID'=>10),

));

findByAttributes 里的

 

$attributes就是字段的名字.

查询title为abc怎么查询呢?见下面

Post::model()->findByAttributes(array('title'=>'

 

abc'))

其它方法(转http://hi.baidu.com/fegro/blog/item/8c8093ff886b8197b801a05f.html ):

1、$admin=Admin::model()->findAll($condition,$params);     
 
该方法是根据一个条件查询一个集合,如: 
findAll("username=:name",array(":name"=>$username));     
 
 
2、$admin=Admin::model()->findAllByPk($postIDs,$condition,$params); 
findAllByPk($id,"name like ':name' and age=:age" ,array(':name'=>$name,'age'=>$age));     
 
该方法是根据主键查询一个集合,可以使用多个主键,如: 
findAllByPk(array(1,2)); 
         
 
3、$admin=Admin::model()->findAllByAttributes($attributes,$condition,$params); 
 
该方法是根据条件查询一个集合,可以是多个条件,把条件放到数组里面,根据属性来查询,就想sql的where里的多个and如
如: 
findAllByAttributes(array('username'=>'admin'));
 
SELECT *  from one_table where username = 'admin' and password = '123456';
 对应的AR model查询如下
 Admin::model()->findAllByAttributes(array('username'=>'admin', 'password'=>'123456')); 
 
 
 
4、$admin=Admin::model()->findAllBySql($sql,$params); 
 
该方法是根据SQL语句查询一个数组,如: 
findAllBySql("select *from admin where username=:name",array(':name'=>'admin')); 
----------------------------------------------------------------------------- 
二、查询对像的方法 
1、$admin=Admin::model()->findByPk($postID,$condition,$params); 
 
根据主键查询出一个对象,如:findByPk(1); 
 
2、$row=Admin::model()->find($condition,$params); 
 
根据一个条件查询出一组数据,可能是多个,但是他只返回第一行数据,如: 
find('username=:name',array(':name'=>'admin')); 
 
3、$admin=Admin::model()->findByAttributes($attributes,$condition,$params); 
 
该方法是根据条件查询一组数据,可以是多个条件,把条件放到数组里面,他查询的也是第一条数据,如: 
findByAttributes(array('username'=>'admin')); 
 
 
4、$admin=Admin::model()->findBySql($sql,$params); 
 
该方法是根据SQL语句查询一组数据,他查询的也是第一条数据,如: 
findBySql("select *from admin where username=:name",array(':name'=>'admin')); 
 
5、拼一个获得SQL的方法,在根据find查询出一个对象 
$criteria=new CDbCriteria; 
$criteria->select='username';  // only select the 'title' column 
$criteria->condition='username=:username'; 
$criteria->params=array(':username=>'admin'); 
$post=Post::model()->find($criteria); // $params is not needed 
 
 
------------------------------------------------------------------------------ 
三、查询个数,判断查询是否有结果 
1、$n=Post::model()->count($condition,$params); 
 
该方法是根据一个条件查询一个集合有多少条记录,返回一个int型数字,如 
count("username=:name",array(":name"=>$username));     
 
2、$n=Post::model()->countBySql($sql,$params); 
 
该方法是根据SQL语句查询一个集合有多少条记录,返回一个int型数字,如 
countBySql("select *from admin where username=:name",array(':name'=>'admin')); 
 
3、$exists=Post::model()->exists($condition,$params); 
 
该方法是根据一个条件查询查询得到的数组有没有数据,如果有数据返回一个true,否则没有找到 
 
 
================================================================================================================= 
四、添加的方法 
$admin=new Admin;         
$admin->username=$username; 
$admin->password=$password; 
if($admin->save()>0){ 
    echo "添加成功"; 
}else{ 
    echo "添加失败"; 
==================================================================================================================== 
五、修改的方法 
1、Post::model()->updateAll($attributes,$condition,$params); 
 
$count = Admin::model()->updateAll(array('username'=>'11111','password'=>'11111'),'password=:pass',array(':pass'=>'1111a1')); 
if($count>0){ 
    echo "修改成功"; 
}else{ 
    echo "修改失败"; 
 
2、Post::model()->updateByPk($pk,$attributes,$condition,$params); 
 
$count = Admin::model()->updateByPk(1,array('username'=>'admin','password'=>'admin')); 
$count = Admin::model()->updateByPk(array(1,2),array('username'=>'admin','password'=>'admin'),'username=:name',array(':name'=>'admin')); 
if($count>0){ 
    echo "修改成功"; 
}else{ 
    echo "修改失败"; 
$pk代表主键,可以是一个也可以是一个集合,$attributes代表是要修改的字段的集合,$condition代表条件,$params传入的值 
 
3、Post::model()->updateCounters($counters,$condition,$params); 
 
$count =Admin::model()->updateCounters(array('status'=>1),'username=:name',array(':name'=>'admin')); 
if($count>0){ 
    echo "修改成功"; 
}else{ 
    echo "修改失败"; 
array('status'=>1)代表数据库中的admin表根据条件username='admin',查询出的所有结果status字段都自加1 
 
================================================================================================================================================ 
六、删除的方法 
1、Post::model()->deleteAll($condition,$params); 
 
$count = Admin::model()->deleteAll('username=:name and password=:pass',array(':name'=>'admin',':pass'=>'admin')); 
            $id=1,2,3 
            deleteAll('id in(".$id.")');删除id为这些的数据 
if($count>0){ 
    echo "删除成功"; 
}else{ 
    echo "删除失败"; 
 
2、Post::model()->deleteByPk($pk,$condition,$params); 
$count = Admin::model()->deleteByPk(1); 
$count = Admin::model()->deleteByPk(array(1,2),'username=:name',array(':name'=>'admin')); 
if($count>0){ 
    echo "删除成功"; 
}else{ 
    echo "删除失败"; 

}

YII AR查询方法的更多相关文章

  1. 如果在Yii中,使用AR查询,不直接写sql,则在使用的时候会报错

    如果在Yii中,使用AR查询,不直接写sql,则在使用的时候会报错 Student::find() ->select("id,name,from_unixtime(create_tim ...

  2. Yii AR Model CRUD数据库操作

    Yii AR很好很强大,但刚开始不知道怎么使用,可以先看下官方文档 官方文档:http://www.yiichina.com/guide/database.ar 下面是我对AR的一些理解 对于一个Mo ...

  3. crontab Yii commands 使用方法

    基本知识介绍 #crontab -u <-l, -r, -e> -u指定一个用户-l列出某个用户的任务计划-r删除某个用户的任务-e编辑某个用户的任务 cron文件语法与写法 Minute ...

  4. tp5 中 model 的查询方法

    实例化模型后调用查询方法,可以写任何想要的查询(推荐) public function select(){ $user = model('User'); $data = $user -> ) - ...

  5. 【Java EE 学习 17 下】【数据库导出到Excel】【多条件查询方法】

    一.导出到Excel 1.使用DatabaseMetaData分析数据库的数据结构和相关信息. (1)测试得到所有数据库名: private static DataSource ds=DataSour ...

  6. <五>JDBC_利用反射及JDBC元数据编写通用的查询方法

    此类针对javaBean类写了一个通用的查询方法,List<javaBean> 通用查询更新中...:通过学习,深刻体会到学会反射就等于掌握了java基础的半壁江山! 一.使用JDBC驱动 ...

  7. Thinkphp回顾之(四)查询方法深入学习

    本次讲的查询方法主要有:表达式查询,模糊查询,between语句,in语句,区间查询,统计数据,普通方式查询,但大多数都只是引入数组而已,明白了第一个,其他的也就差不多全明白了,唯一要注意的是在后台中 ...

  8. YII 伪静态 IIS7 方法 web.config

    YII 伪静态 IIS7 方法 web.config <?xml version="1.0" encoding="UTF-8"?> <conf ...

  9. MyBaits一对一的查询方法

    MyBaits一对一的查询方法 一:表数据与表结构 CREATE TABLE teacher( t_id INT PRIMARY KEY AUTO_INCREMENT, t_name ) ); CRE ...

随机推荐

  1. Sql Server Profiler保存与重演跟踪

    重演跟踪的作用 可以将一个跟踪当做测试工具,当按照正确的顺序调用某些存储过程是肯能会重新生成特定的故障. 跟踪模板

  2. 对象创建型模式------Builder(生成器或建造者模式)(2)

    链接转自:http://blog.csdn.net/wuzhekai1985/article/details/6667467 主要思想是:首先有个指挥家思想者将大体的设计思路设计出来,然后寻找一部分工 ...

  3. Google Map JavaScript API V3 实例大全

    Google Map JavaScript API V3 实例大全 基础知识 简单的例子 地理位置 语言 位置 坐标 简单的投影 事件 简单事件 关闭事件 多次添加事件 事件属性 控制 php禁用ui ...

  4. C# 打印多页tif

    注意点: 1.计算image对象总页数 image.GetFrameCount(FrameDimension.Page); 2.初始化当前页,并获取指定页内容 image.SelectActiveFr ...

  5. (转)安装程序发布利器——InstallShield 2011 Limited Edition

    最近经常写WCF服务和Windows服务,之前知道可以通过vs2010自带的“安装项目”可以发布程序,但是自vs2010起,同时提供了InstallShield LE. 下面我们通过图示,来了解Ins ...

  6. bzoj1015:[JSOI2008]星球大战starwar

    思路:反着做用并查集维护连通块个数就好了. #include<iostream> #include<cstdio> #include<cstring> #inclu ...

  7. linux 判断指定用户对指定目录具有的权限

    脚本名:power.sh 脚本内容: ------------------------------------------- 注意:必须以root 身份执行该脚本. 脚本power.sh 需要两个参数 ...

  8. c++中动态尾随内存的技巧和定位new

    c 和 c++ 最大的特点就是对内存的自由操作,数据类型,其实都是对内存的一种解释方式.C语言中常用的一个技巧就是尾随数据,网络编程中经常会用到这个特性, 特别是以前写完成端口的时候,这个特性肯定是会 ...

  9. Javascript 中 null、NaN和undefined的区别

    1.类型分析: js中的数据类型有undefined,boolean,number,string,object等5种,前4种为原始类型,第5种为引用类型. 代码 var a1; var a2 = tr ...

  10. DataGridView默认不选中

    NurseGridList.CurrentCell = null;                NurseGridList.ClearSelection();                Nurs ...