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. 编程中的 if ()else() 语句

    例句    if()  else() double d = Convert.ToDouble(Console .ReadLine()); if (d >= 60 && d< ...

  2. php ticks 调试应用

    declare(ticks=1); register_tick_function('do_profile'); register_shutdown_function('show_profile'); ...

  3. selenium文件上传的实现

    一.对于上传文件, 从手动操作我们可以看出, 需要对window 窗体进行操作, 而对于selenium webdriver 在这方面应用就受到了限制. 但是, 庆幸的是, 对于含有input ele ...

  4. 【我们都爱Paul Hegarty】斯坦福IOS8公开课个人笔记1 IOS8概述

    首先感谢网易公开课和SwiftV课堂的朋友们辛苦翻译,这个系列是我学习斯坦福IOS8公开课的个人心得体会和笔记,希望能给大家带来启发. 首先我们要知道IOS系统中的结构情况,从贴近硬件的底层到贴近用户 ...

  5. 三【相关度 相似度查询与计算】相似度到大数据查找之Mysql 文章匹配的一些思路与提高查询速度

    记录下,在上2回的数据基础之上,附带一个互信息(MI,Mutual Information)可以计算词之间的相关度 标准互信息 MI(X,Y)=log2p(x,y)/p(x)p(y) 值越大于0 则趋 ...

  6. 使用jQuery调用ASP.NET WebService的简易教程

    鉴于使用Javascript调用Web Service配置略麻烦,所以记录一下. 1. 新建一个Web服务(WebService.asmx) 2. 取消注释// [System.Web.Script. ...

  7. Java 与 Python 的对比

    最近在学习Python, 现在写一个Python程序和Java程序进行对一下比,以此展示各自不同的特点.这个程序的功能是计算([n, m) )之间的闰年.     Python程序如下: def fu ...

  8. 24种设计模式--访问者模式【Visitor Pattern】

    今天天气不错,绝对是晴空万里,骄阳似火呀,好,我们今天来讲访问者模式,我们在前面讲了组合模式和迭代器模式,通过组合模式我们能够把一个公司的人员组织机构树搭建起来,给管理带来非常大的便利,通过迭代器模式 ...

  9. mysql主配置文件my.cnf详细说明

    MySQL配置文件my.cnf 例子最详细翻译,可以保存做笔记用[转载]#BEGIN CONFIG INFO#DESCR: 4GB RAM, 只使用InnoDB, ACID, 少量的连接, 队列负载大 ...

  10. 路由器无线桥接 router wireless bridge

    实验环境:TP-Link A,TP-Link B,两个路由器都有子网,分别为子网 A,子网 B.TP-Link A连接学校子网 IP A,TP-Link B连接学校子网 IP B.两个路由器都能够通过 ...