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 图片上传实现代码 带详细注释
Apr 29 PHP
编译php 5.2.14+fpm+memcached(具体操作详解)
Jun 18 PHP
Destoon模板制作简明教程
Jun 20 PHP
Discuz批量替换帖子内容的方法(使用SQL更新数据库)
Jun 23 PHP
php实现压缩多个CSS与JS文件的方法
Nov 11 PHP
PHP自带ZIP压缩、解压缩类ZipArchiv使用指南
Mar 03 PHP
基于PHP实现的事件机制实例分析
Jun 18 PHP
PHP页面输出搜索后跳转下一页的处理方法
Sep 30 PHP
Laravel 5.4因特殊字段太长导致migrations报错的解决
Oct 22 PHP
ThinkPHP框架实现的邮箱激活功能示例
Jun 15 PHP
mongodb和php的用法详解
Mar 25 PHP
php优化查询foreach代码实例讲解
Mar 24 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原理之Session Gc的一个小概率Notice
2011/04/12 PHP
浅析ThinkPHP中execute和query方法的区别
2014/06/13 PHP
在云虚拟主机部署thinkphp5项目的步骤详解
2017/12/21 PHP
PHP正则表达式函数preg_replace用法实例分析
2020/06/04 PHP
表单的一些基本用法与技巧
2006/07/15 Javascript
国外大牛IE版本检测!现在IE都到9了,IE检测代码
2012/01/04 Javascript
JavaScript中对循环语句的优化技巧深入探讨
2014/06/06 Javascript
Javascript基础教程之比较操作符
2015/01/18 Javascript
基于jQuery实现网页进度显示插件
2015/03/04 Javascript
50 个 jQuery 插件可将你的网站带到另外一个高度
2016/04/26 Javascript
基于WebUploader的文件上传js插件
2016/08/19 Javascript
js实现右键菜单功能
2016/11/28 Javascript
jQuery控制元素隐藏和显示
2017/03/03 Javascript
JavaScript实现二叉树的先序、中序及后序遍历方法详解
2017/10/26 Javascript
vue.js vue-router如何实现无效路由(404)的友好提示
2017/12/20 Javascript
JS实现的简单下拉框联动功能示例
2018/05/11 Javascript
vue实现Input输入框模糊查询方法
2021/01/29 Javascript
小程序自定义导航栏兼容适配所有机型(附完整案例)
2020/04/26 Javascript
[06:23]2014DOTA2西雅图国际邀请赛 小组赛7月12日TOPPLAY
2014/07/12 DOTA
[42:32]完美世界DOTA2联赛PWL S2 LBZS vs FTD.C 第二场 11.27
2020/12/01 DOTA
pycharm中连接mysql数据库的步骤详解
2017/05/02 Python
用Python编写一个简单的CS架构后门的方法
2018/11/20 Python
Python获取航线信息并且制作成图的讲解
2019/01/03 Python
django将数组传递给前台模板的方法
2019/08/06 Python
Python中*args和**kwargs的区别详解
2019/09/17 Python
Python 实现将某一列设置为str类型
2020/07/14 Python
HTML5 Canvas 起步(1) - 基本概念
2009/05/12 HTML / CSS
意大利香水和彩妆护肤品购物网站:Ditano
2017/08/13 全球购物
乌克兰电子和家用电器商店:Foxtrot
2019/07/23 全球购物
德国苹果商店:MacTrade
2020/05/18 全球购物
工作评语大全
2014/04/26 职场文书
2014年学校后勤工作总结
2014/12/06 职场文书
医务人员医德考评自我评价
2015/03/03 职场文书
具结保证书范本
2015/05/11 职场文书
在js中修改html body的样式
2021/11/11 Javascript
MySQL Innodb索引机制详细介绍
2021/11/23 MySQL