入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

Yii AR Model 查询

创建时间:2013-03-08 投稿人: 浏览次数:129

如果英文不错,可以直接看原文地址http://www.yiiframework.com/doc/guide/1.1/en/database.ar

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

//查找满足指定条件的结果中的第一行 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);//未找到返回null

//通过指定的SQL 语句查找结果中的第一行 find the first row using the specified SQL statement
$post=Post::model()->findBySql($sql,$params);

如果find 方法找到了一个满足查询条件的行,它将返回一个Post 实例,实例的属性含有数据表行中相应列的值。然后我们就可以像读取普通对象的属性那样读取载入的值,例如echo $post->title;。如果使用给定的查询条件在数据库中没有找到任何东西, find 方法将返回null。

调用find 时,我们使用$condition 和$params 指定查询条件。此处$condition 可以是SQL 语句中的WHERE 字符串,$params 则是一个参数数组,其中的值应绑定到$condation 中的占位符。例如:假设我们查询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 

一种替代CDbCriteria 的方法是给find 方法传递一个数组。数组的键和值各自对应标准( criterion)的属性名和值,上面的例子可以重写为如下:

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

当然也适用于findAll()

self::$_items[$type]=array();
$models=self::model()->findAll(array(
    "condition"=>"type=:type",
    "params"=>array(":type"=>$type),
    "order"=>"position",
));
当一个查询条件是关于按指定的值匹配几个列时,我们可以使用findByAttributes()。我们使$attributes 参数是一个以列名做索引的值的数组。

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); 
 
该方法是根据条件查询一个集合,可以是多个条件,把条件放到数组里面,如: 
findAllByAttributes(array("username"=>"admin")); 
 
 
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 "删除失败"; 








声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。