首页 技术 正文
技术 2022年11月6日
0 收藏 436 点赞 466 浏览 2731 个字

http://www.php.cn/php/php-getBy.html

根据字段名动态查询:getBy字段名( )

该方法很有意思,手册的说得很简略,我们根据源码来好好说道说道~~

1. 功能:根据字段值查询记录,且仅返回一条记录

2. 源码:/thinkphp/library/think/db/Query.php

您可能感到奇怪,我们不是在学习模型吗?怎么又折腾回了数据库了?原因很简单:这个动态查询是通过Query类的构造方法实现的:

  • Query.php 构造方法源码:
    /**
     * 利用__call方法实现一些特殊的Model方法
     * @access public
     * @param string $method 方法名称
     * @param array  $args   调用参数
     * @return mixed
     * @throws DbException
     * @throws Exception
     */public function __call($method, $args){if (strtolower(substr($method, 0, 5)) == 'getby') {// 根据某个字段获取记录$field         = Loader::parseName(substr($method, 5));$where[$field] = $args[0];return $this->where($where)->find();
        } elseif (strtolower(substr($method, 0, 10)) == 'getfieldby') {// 根据某个字段获取记录的某个值$name         = Loader::parseName(substr($method, 10));$where[$name] = $args[0];return $this->where($where)->value($args[1]);
        } else {throw new Exception('method not exist:' . __CLASS__ . '->' . $method);
        }
    }
  • 与getBy字段名( )相关的代码段:

注释是本人所加,源代码中并没有

  //去掉getBy获取字段名,并转为小写后,判断是否存在字段名字符串
  if (strtolower(substr($method, 0, 5)) == 'getby') {// 根据某个字段获取记录$field         = Loader::parseName(substr($method, 5));//将getBy***()的参数做为where方法的参数$where[$field] = $args[0];//返回查询结果:只返回单条满足条件的记录return $this->where($where)->find();

3. 实例演示:

  • 老规矩,先创建一个模型类:/application/index/mode/Staff.php
<?php
namespace app\index\model;//导入模型类use think\model;class Staff extends model {//自定义模型类代码}

一、任务1:查询姓名name等于”李云龙”的记录

  • 控制器:/application/index/controller/Index.php
<?phpnamespace app\index\controller;//导入模型类use app\index\model\Staff;class Index {  public function index(){  //1.获取name='李云龙'的【数据对象】
  $result = Staff::getByName('李云龙');  //2.获取数据对象原始数据
  $data = $result -> getData();     //3.查看结果
  dump($datas);  
  }
}
  • 查看结果:
array(7) {
  ["id"] => int(1011)
  ["name"] => string(9) "李云龙"
  ["sex"] => int(1)
  ["age"] => int(49)
  ["salary"] => float(10350)
  ["dept"] => int(1)
  ["hiredate"] => string(10) "2005-10-20"}

如果查询其它字段,只需要修改一下方法名称中的字段名部分即可,如查询年龄=30岁,方法名就是:getByAge(30)。因为方法名称因字段名称变化而变化,所以要动态查询。

二、任务2:查询姓名中含有:’大’字的员工信息

getBy字段名( )方法参数是否支持查询表达式呢?手册并未提及,但是看了源码后,发现有限支持:数组式查询表达式,如:[ ‘between’,[1010,1020] ] 或者:[ ‘>’,1020 ]

  • 控制器:/application/index/controller/Index.php
<?phpnamespace app\index\controller;//导入模型类use app\index\model\Staff;class Index {  public function index(){  //1.获取name中包含"大"字符的记录,返回【数据对象】
  $result = Staff::getByName(['like','%大%']);  //2.获取数据对象原始数据
  $data = $result -> getData();     //3.查看结果
  dump($data);  
  }
}
  • 查看结果
array(7) {
  ["id"] => int(1024)
  ["name"] => string(9) "鲁大师"
  ["sex"] => int(0)
  ["age"] => int(60)
  ["salary"] => float(1300)
  ["dept"] => int(2)
  ["hiredate"] => string(10) "2012-09-09"}

4. 总结:

尽管动态查询方法的参数支持查询表达式,但不要比较或范围表达式,因为仅返回第一条满足条件记录,通常是没有意义的。模糊查询有时很有用,但同样也仅获取结果集的首条记录

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