Yii基于数组和对象的Model查询技巧实例详解


Posted in PHP onDecember 28, 2015

本文实例讲述了Yii基于数组和对象的Model查询技巧。分享给大家供大家参考,具体如下:

对于一个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'))

其它方法:

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 "删除失败";
}

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

PHP 相关文章推荐
PHP脚本数据库功能详解(上)
Oct 09 PHP
一个可以删除字符串中HTML标记的PHP函数
Oct 09 PHP
Bo-Blog专用的给Windows服务器的IIS Rewrite程序
Aug 26 PHP
PHP中删除变量时unset()和null的区别分析
Jan 27 PHP
apache+php完美解决301重定向的两种方法
Jun 08 PHP
深入PHP nl2br()格式化输出的详解
Jun 05 PHP
解密ThinkPHP3.1.2版本之独立分组功能应用
Jun 19 PHP
PHP递归遍历指定目录的文件并统计文件数量的方法
Mar 24 PHP
微信网页授权(OAuth2.0) PHP 源码简单实现
Aug 29 PHP
php微信开发之自定义菜单完整流程
Oct 08 PHP
详解yii2实现分库分表的方案与思路
Feb 03 PHP
PHP调用接口API封装的例子
Oct 11 PHP
yii权限控制的方法(三种方法)
Dec 28 #PHP
Yii使用Captcha验证码的方法
Dec 28 #PHP
yii使用activeFileField控件实现上传文件与图片的方法
Dec 28 #PHP
yii实现使用CUploadedFile上传文件的方法
Dec 28 #PHP
Yii中Model(模型)的创建及使用方法
Dec 28 #PHP
yii数据库的查询方法
Dec 28 #PHP
yii分页组件用法实例分析
Dec 28 #PHP
You might like
php xml文件操作代码(一)
2009/03/20 PHP
php获取url字符串截取路径的文件名和扩展名的函数
2010/01/22 PHP
jQuery源码分析-04 选择器-Sizzle-工作原理分析
2011/11/14 Javascript
jQuery实现单行文字间歇向上滚动源代码
2013/06/02 Javascript
JS 打印功能代码可实现打印预览、打印设置等
2014/10/31 Javascript
javascript 实现 原路返回
2015/01/21 Javascript
jquery实现TAB选项卡鼠标经过带延迟效果的方法
2015/07/27 Javascript
JS中Eval解析JSON字符串的一个小问题
2016/02/21 Javascript
js验证框架之RealyEasy验证详解
2016/06/08 Javascript
jQuery替换节点用法示例(使用replaceWith方法)
2016/09/08 Javascript
js 实现一些跨浏览器的事件方法详解及实例
2016/10/27 Javascript
JS传参及动态修改页面布局
2017/04/13 Javascript
JS异步函数队列功能实例分析
2017/11/28 Javascript
vue 框架下自定义滚动条(easyscroll)实现方法
2019/08/29 Javascript
js利用递归与promise 按顺序请求数据的方法
2019/08/30 Javascript
Python之str操作方法(详解)
2017/06/19 Python
Selenium定位元素操作示例
2018/08/10 Python
Python利用递归实现文件的复制方法
2018/10/27 Python
Django Rest framework之认证的实现代码
2018/12/17 Python
django中使用事务及接入支付宝支付功能
2019/09/15 Python
Python实现快速排序的方法详解
2019/10/25 Python
Django3中的自定义用户模型实例详解
2020/08/23 Python
巴西婴儿用品商店:Bebe Store
2017/11/23 全球购物
Three Graces London官网:英国奢侈品牌
2021/03/18 全球购物
在数据文件自动增长时,自动增长是否会阻塞对文件的更新
2014/05/01 面试题
竞争上岗演讲稿
2014/01/05 职场文书
餐饮管理自我介绍信
2014/01/15 职场文书
大学生军训广播稿
2014/01/24 职场文书
员工晚婚的请假条
2014/02/08 职场文书
新年抽奖获奖感言
2014/03/02 职场文书
商铺消防安全责任书
2014/07/29 职场文书
关于工作时间玩手机的检讨书
2014/09/18 职场文书
走群众路线剖析材料
2014/10/09 职场文书
行政前台岗位职责
2015/04/16 职场文书
Python使用protobuf序列化和反序列化的实现
2021/05/19 Python
用Python实现一个打字速度测试工具来测试你的手速
2021/05/28 Python