章节索引 :

获取数据追加字段

1. 前言

前面小节介绍了数据库操作,本小节介绍如何在数据模型中指定追加字段,追加字段的目的主要是用于对某些字段的特殊处理或需要在返回数据的时候返回某些特性情况的字段,这样的处理对接口的使用者来说更加友好,对于开发者来说可以减少代码量和工作量。

2. 应用场景

下面返回的数据可以看到创建时间字段 created_at 是时间戳,这种就需要转化成可读的日期格式,这个时候就可以用到 ThinkPHP 提供的数据模型的字段追加功能,它可以在原有字段的基础上追加出想要的其他字段数据。之前获取学生列表接口的时候返回数据如下:

{
    "total": 200,
    "per_page": 10,
    "current_page": 2,
    "last_page": 20,
    "data": [
        {
            "id": 12,
            "name": "孙空",
            "age": 20,
            "id_number": "420117201005127996",
            "created_at": 1603617951,
            "update_at": 0,
            "status": 1
        },
        {
            "id": 1,
            "name": "赵四",
            "age": 24,
            "id_number": "420117201005124146",
            "created_at": 1603617951,
            "update_at": 0,
            "status": 1
        },
        {
            "id": 10,
            "name": "孙空",
            "age": 21,
            "id_number": "420117201005124671",
            "created_at": 1603617951,
            "update_at": 0,
            "status": 1
        },
        {
            "id": 9,
            "name": "钱学",
            "age": 19,
            "id_number": "420117201005121149",
            "created_at": 1603617951,
            "update_at": 0,
            "status": 1
        },
        {
            "id": 8,
            "name": "吴小明",
            "age": 21,
            "id_number": "420117201005123197",
            "created_at": 1603617951,
            "update_at": 0,
            "status": 1
        },
        {
            "id": 7,
            "name": "张红",
            "age": 19,
            "id_number": "420117201005123721",
            "created_at": 1603617951,
            "update_at": 0,
            "status": 1
        },
        {
            "id": 6,
            "name": "王五",
            "age": 25,
            "id_number": "420117201005123617",
            "created_at": 1603617951,
            "update_at": 0,
            "status": 1
        },
        {
            "id": 5,
            "name": "钱学",
            "age": 23,
            "id_number": "420117201005123085",
            "created_at": 1603617951,
            "update_at": 0,
            "status": 1
        },
        {
            "id": 4,
            "name": "赵四",
            "age": 20,
            "id_number": "420117201005128637",
            "created_at": 1603617951,
            "update_at": 0,
            "status": 1
        },
        {
            "id": 3,
            "name": "赵四",
            "age": 18,
            "id_number": "420117201005125711",
            "created_at": 1603617951,
            "update_at": 0,
            "status": 1
        }
    ]
}

3. 设置追加字段

这里还是以之前第 10 小节学生列表为例,可以在 app\Models\Study\StudentModel 中增加属性 $append,然后想要追加什么字段可以初始化 $append,如下图所示:
图片描述

此时再去请求之前的分页列表接口,会出现如下 created_at_text 字段,因为没有处理该值,所以显示的是 null

{
    "total": 400,
    "per_page": 15,
    "current_page": 1,
    "last_page": 27,
    "data": [
        {
            "id": 205,
            "name": "吴小明",
            "age": 21,
            "id_number": "420117201005129466",
            "created_at": 1605286706,
            "update_at": 0,
            "status": 1,
            "created_at_text": null
        },
        {
            "id": 212,
            "name": "孙空",
            "age": 18,
            "id_number": "420117201005127426",
            "created_at": 1605286706,
            "update_at": 0,
            "status": 1,
            "created_at_text": null
        },
        {
            "id": 208,
            "name": "张红",
            "age": 22,
            "id_number": "420117201005126184",
            "created_at": 1605286706,
            "update_at": 0,
            "status": 1,
            "created_at_text": null
        },
        {
            "id": 206,
            "name": "王五",
            "age": 21,
            "id_number": "420117201005124940",
            "created_at": 1605286706,
            "update_at": 0,
            "status": 1,
            "created_at_text": null
        },
        {
            "id": 215,
            "name": "赵四",
            "age": 20,
            "id_number": "420117201005122353",
            "created_at": 1605286706,
            "update_at": 0,
            "status": 1,
            "created_at_text": null
        },
        {
            "id": 213,
            "name": "张红",
            "age": 20,
            "id_number": "420117201005126460",
            "created_at": 1605286706,
            "update_at": 0,
            "status": 1,
            "created_at_text": null
        },
        {
            "id": 204,
            "name": "吴小明",
            "age": 19,
            "id_number": "420117201005124822",
            "created_at": 1605286706,
            "update_at": 0,
            "status": 1,
            "created_at_text": null
        },
        {
            "id": 209,
            "name": "李珍",
            "age": 19,
            "id_number": "420117201005122985",
            "created_at": 1605286706,
            "update_at": 0,
            "status": 1,
            "created_at_text": null
        },
        {
            "id": 207,
            "name": "钱学",
            "age": 24,
            "id_number": "420117201005123220",
            "created_at": 1605286706,
            "update_at": 0,
            "status": 1,
            "created_at_text": null
        },
        {
            "id": 203,
            "name": "孙空",
            "age": 19,
            "id_number": "420117201005129634",
            "created_at": 1605286706,
            "update_at": 0,
            "status": 1,
            "created_at_text": null
        },
        {
            "id": 211,
            "name": "张红",
            "age": 22,
            "id_number": "420117201005129909",
            "created_at": 1605286706,
            "update_at": 0,
            "status": 1,
            "created_at_text": null
        },
        {
            "id": 400,
            "name": "孙空",
            "age": 23,
            "id_number": "420117201005129673",
            "created_at": 1605286706,
            "update_at": 0,
            "status": 1,
            "created_at_text": null
        },
        {
            "id": 214,
            "name": "王五",
            "age": 20,
            "id_number": "420117201005126275",
            "created_at": 1605286706,
            "update_at": 0,
            "status": 1,
            "created_at_text": null
        },
        {
            "id": 210,
            "name": "钱学",
            "age": 18,
            "id_number": "420117201005125767",
            "created_at": 1605286706,
            "update_at": 0,
            "status": 1,
            "created_at_text": null
        },
        {
            "id": 202,
            "name": "李珍",
            "age": 20,
            "id_number": "420117201005125731",
            "created_at": 1605286706,
            "update_at": 0,
            "status": 1,
            "created_at_text": null
        }
    ]
}

如下图所示:
图片描述

4.定义追加字段赋值方法

可以在 StudentModel 中创建如下方法处理追加字段值:

    public function getCreatedAtTextAttr()
    {
        return date('Y-m-d H:i',$this->created_at);
    }

如下图所示:
图片描述

Tips: 追加字段处理的方法命名格式为 getAaaBbbAttr() 这种格式,其中 AaaBbb 是追加字段按照大驼峰命名书写的。

5. 请求数据查看

postman 请求学生列表接口,可以查看到数据中已有可读的 created_at_text 字段:

{
    "total": 400,
    "per_page": 15,
    "current_page": 1,
    "last_page": 27,
    "data": [
        {
            "id": 205,
            "name": "吴小明",
            "age": 21,
            "id_number": "420117201005129466",
            "created_at": 1605286706,
            "update_at": 0,
            "status": 1,
            "created_at_text": "2020-11-14 00:58"
        },
        {
            "id": 212,
            "name": "孙空",
            "age": 18,
            "id_number": "420117201005127426",
            "created_at": 1605286706,
            "update_at": 0,
            "status": 1,
            "created_at_text": "2020-11-14 00:58"
        },
        {
            "id": 208,
            "name": "张红",
            "age": 22,
            "id_number": "420117201005126184",
            "created_at": 1605286706,
            "update_at": 0,
            "status": 1,
            "created_at_text": "2020-11-14 00:58"
        },
        {
            "id": 206,
            "name": "王五",
            "age": 21,
            "id_number": "420117201005124940",
            "created_at": 1605286706,
            "update_at": 0,
            "status": 1,
            "created_at_text": "2020-11-14 00:58"
        },
        {
            "id": 215,
            "name": "赵四",
            "age": 20,
            "id_number": "420117201005122353",
            "created_at": 1605286706,
            "update_at": 0,
            "status": 1,
            "created_at_text": "2020-11-14 00:58"
        },
        {
            "id": 213,
            "name": "张红",
            "age": 20,
            "id_number": "420117201005126460",
            "created_at": 1605286706,
            "update_at": 0,
            "status": 1,
            "created_at_text": "2020-11-14 00:58"
        },
        {
            "id": 204,
            "name": "吴小明",
            "age": 19,
            "id_number": "420117201005124822",
            "created_at": 1605286706,
            "update_at": 0,
            "status": 1,
            "created_at_text": "2020-11-14 00:58"
        },
        {
            "id": 209,
            "name": "李珍",
            "age": 19,
            "id_number": "420117201005122985",
            "created_at": 1605286706,
            "update_at": 0,
            "status": 1,
            "created_at_text": "2020-11-14 00:58"
        },
        {
            "id": 207,
            "name": "钱学",
            "age": 24,
            "id_number": "420117201005123220",
            "created_at": 1605286706,
            "update_at": 0,
            "status": 1,
            "created_at_text": "2020-11-14 00:58"
        },
        {
            "id": 203,
            "name": "孙空",
            "age": 19,
            "id_number": "420117201005129634",
            "created_at": 1605286706,
            "update_at": 0,
            "status": 1,
            "created_at_text": "2020-11-14 00:58"
        },
        {
            "id": 211,
            "name": "张红",
            "age": 22,
            "id_number": "420117201005129909",
            "created_at": 1605286706,
            "update_at": 0,
            "status": 1,
            "created_at_text": "2020-11-14 00:58"
        },
        {
            "id": 400,
            "name": "孙空",
            "age": 23,
            "id_number": "420117201005129673",
            "created_at": 1605286706,
            "update_at": 0,
            "status": 1,
            "created_at_text": "2020-11-14 00:58"
        },
        {
            "id": 214,
            "name": "王五",
            "age": 20,
            "id_number": "420117201005126275",
            "created_at": 1605286706,
            "update_at": 0,
            "status": 1,
            "created_at_text": "2020-11-14 00:58"
        },
        {
            "id": 210,
            "name": "钱学",
            "age": 18,
            "id_number": "420117201005125767",
            "created_at": 1605286706,
            "update_at": 0,
            "status": 1,
            "created_at_text": "2020-11-14 00:58"
        },
        {
            "id": 202,
            "name": "李珍",
            "age": 20,
            "id_number": "420117201005125731",
            "created_at": 1605286706,
            "update_at": 0,
            "status": 1,
            "created_at_text": "2020-11-14 00:58"
        }
    ]
}

如下图所示:
图片描述

6. 小结

本小节主要介绍如何在模型中追加字段,只需要在模型设置 protected $append 字段属性即可,需要追加的字段需要定义对应的 getAaBbAttr() 方法对字段赋值,通过追加字段的方式可以很方便的处理某些特定的字段。

Tips: 代码仓库:https://gitee.com/love-for-poetry/tp6