Yii Model查询技巧概述
本例深入解析Yii框架中基于数组与对象的Model查询技巧,供开发者参考借鉴。
以Post模型为例,存在四种主要的查询方式,分别返回单个对象实例或对象数组。
//查找满足指定条件的结果中的第一行 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输出标题。若未找到任何记录,该方法将返回null。
调用find时需传入$condition和$params两个参数。$condition通常对应SQL语句中的WHERE子串,而$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
除了使用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'))
其他常用查询方法包括:
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,否则返回false。
四、添加记录的方法
$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框架的PHP程序设计。