FastAdmin Thinkphp5 中关于 whereTime不生效的一个小细节

f239415288b4465dbbb98c78a0110b76_1012962761

数据表如下

create table fa_test
(
    id           int unsigned auto_increment comment 'ID'
        primary key,
    user_id      int(10)                           default 0        null comment '会员ID',
    year         year                                               null comment '年',
    times        time                                               null comment '时间',
    refreshtime  bigint(16)                                         null comment '刷新时间',
    createtime   bigint(16)                                         null comment '创建时间',
    updatetime   bigint(16)                                         null comment '更新时间',
    deletetime   bigint(16)                                         null comment '删除时间',
    delete_time  int(10)                                            null
)
    comment '测试表' collate = utf8mb4_unicode_ci;

源代码如下

$startTime='2022-09-01 00:00:00';
            $end='2022-09-01 00:00:00';
            $range=[$startTime,$end]; //定义个时间区间
            //这里关联的定义为
    /** 
public function cate()
    {
        return $this->belongsTo(Category::class, 'cate_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
*/

            $list = $this->model
                ->with(['cate'])
                ->whereTime('updatetime','between',$range)
                ->whereTime('createtime','between',$range)
                ->whereTime('delete_time','between',$range)
                ->whereTime('time','between',$range)
                ->whereTime('year','between',$range)
                ->whereTime('refreshtime','between',$range)
                ->whereTime('cate.createtime','between',$range)
                ->buildSql();

            //打印执行的sql语句
            print_r($list);

输入结果为

SELECT *
 FROM `fa_test``test`
 LEFT JOIN `fa_category``cate` ON `test`.`cate_id` = `cate`.`id`
 WHERE (
              `updatetime` BETWEEN 1661961600 AND 1661961600 AND
              `createtime` BETWEEN 1661961600 AND 1661961600 AND
              `delete_time` BETWEEN 1661961600 AND 1661961600 AND
              `time` BETWEEN '2022-09-01 00:00:00' AND '2022-09-01 00:00:00' AND
             `year`BETWEEN '1661961600'AND '1661961600' AND
              `refreshtime` BETWEEN 1661961600 AND 1661961600 AND
              `cate`.`createtime` BETWEEN '1661961600' AND '1661961600'
 )

结论

  1. whereTime 的字段,必须为数据表中存在的字段,才会解析为时间戳;
  2. 对于关联模型的字段有同样的要求;
  3. 无论数据表中的字段类型是int还是其他类型, 都会解析为时间戳
© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享