TP5多条件whereOr查询

神通鬼大TP5多条件whereOr查询
问题背景:最近在⽤ThinkPHP 5开发项⽬的过程中,发现根据筛选条件做or查询的时候,连贯操作不可以使⽤where进⾏条件查询了。
⾸先列出⼀个user数据表的信息:
uid uname grade(年级)class(班级)sex(性别)
11号121
22号112
33号332
44号421
55号251
氰乙酸乙酯
66号162
77号111
88号231
99号221
1010号312
数据表展现了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.phpifs系统
<?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

本文发布于:2024-09-23 12:33:48,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/153180.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:发现   查询   班级
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议