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. 40个Java集合面试问题和答案【下】【转载】

    接上文:http://www.cnblogs.com/xujianbo/p/5148083.html 28.哪些集合类是线程安全的? Vector.HashTable.Properties和Stack ...

  2. ReactiveCocoa入门教程——第一部分(转)

    作为一个iOS开发者,你写的每一行代码几乎都是在响应某个事件,例如按钮的点击,收到网络消息,属性的变化(通过KVO)或者用户位置的变化(通过CoreLocation).但是这些事件都用不同的方式来处理 ...

  3. TortoiseSVN本地代码版本控制设置步骤。

    1.下载安装TortoiseSVN客户端. 2.在某个盘创建空的文件夹作为项目代码的版本库.在空的文件夹内部右键鼠标TortoiseSVN->Create repository here 3.在 ...

  4. [Guava官方文档翻译] 2.使用和避免使用null (Using And Avoiding Null Explained)

    本文地址:http://www.cnblogs.com/hamhog/p/3536647.html "null很恶心." -Doug Lea "这是一个令我追悔莫及的错误 ...

  5. java.util.AbstractStringBuilder源码分析

    AbstractStringBuilder是一个抽象类,是StringBuilder和StringBuffer的父类,分析它的源码对StringBuilder和StringBuffer代码的理解有很大 ...

  6. centos 安装qrcode  二维码

    先安装yum install  mingw64-pkg-config.x86_64 yum install cairo-devel 然后报错,好像是gcc版本有点低,现在的版本是4.4.7 那么接下来 ...

  7. Linux – RedHat7 / CentOS 7 忘记root密码修改

    1.(a) 开机出现grub boot loader 开机选项菜单时,立即点击键盘任意鍵,boot loader 会暂停. (b) 按下’e’,编辑选项菜单(c) 移动上下鍵至linux16 核心命令 ...

  8. 排序算法SIX:冒泡排序BubbleSort

    /** *冒泡排序: * 两个两个比较,一轮过后最大的排在了最后面 * n个数变为n-1个没排好的数 * 再进行一轮 * 第二大的排在了倒数第二个 * 以此类推 * 直到排到第一个为止 * * 弄两个 ...

  9. linux 定时执行 cron指令

    linux 中的 cron 定时执行命令,先上例子:每间隙两分钟把 "Hello world"写到 /tmp/hello.txt crontab -e */2 * * * * ec ...

  10. Mac OS X下GnuPlot的安装和配置(无法set term png等图片输出)

    今天使用gitstats分析git repo的活动信息,发现其内部使用gnuplot,结果发现无法生成png图片,进入gnuplot的shell发现无法设置png格式输出.如下 gnuplot> ...