首页 技术 正文
技术 2022年11月20日
0 收藏 985 点赞 4,603 浏览 1125 个字

问题背景:最近在用ThinkPHP 5开发项目的过程中,发现根据筛选条件做or查询的时候,连贯操作不可以使用where进行条件查询了。

首先列出一个user数据表的信息:

uid

uname

grade(年级)

class(班级)

sex(性别)

1

1号

1

2

1

2

2号

1

1

2

3

3号

3

3

2

4

4号

4

2

1

5

5号

2

5

1

6

6号

1

6

2

7

7号

1

1

1

8

8号

2

3

1

9

9号

2

2

1

10

10号

3

1

2

数据表展现了10位同学的年级、班级、性别信息

现在要查询数据为

grade=1 or class= or sex=2

在TP3中想要or查询

条件可以为:

$condition[‘grade’] = 1;

$condition[‘class’] = 3;

$condition[‘sex’] = 2;

$condtion[‘_logic’] = ‘OR’;

$list = M(‘user’)->where($condtion)->findall();

然后在TP5中尝试用where去这么查询发现一直在报错,查了手册之后发现TP5取消了_logic作为查询方式,而是新增了whereOr方法,下面是TP5中查询方式

User.php

<?php
namespace app\index\controller;use app\index\model\UserModel;class User
{
public function index()
{
$condition['grade'] = 1;
$condition['class'] = 3;
$condition['sex'] = 2;
$UserModel = new UserModel;
$list = $UserModel->getlistwhereOr($condition); print_r($list);
}
}

UserModel.php

<?php
namespace app\index\model;
use app\common\model\CommonModel;
use think\Db;
use think\Model;class UserModel extends CommonModel
{
public function __construct(){
parent::__construct(); } protected $name = 'User'; public function getlistwhereOr($condition) {
$list =Db::name($this->name)->whereOr($condition)->select();
return $list;
}
}

执行User.php 发现打印出来的数据就是我们要的筛选数据,

总结:TP5相比TP3中更新了很多我们经常用到的查询方式,而且写法更人性化,需要经常的去整理查看这些新方法

by as

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,557
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898