thinkphp6.0 利用模型事件 实现查询数据后根据需求替换某列的内容 如订单状态字符转文字等等
<?php
namespace app\index\model;
use think\Model;
class OrderTable extends Model
{
protected $table = "order";
protected $pk = "order_id";
public static function onAfterRead($order)
{
$state = $order->state;//得到状态列内容
$statestr = '已下单';
//判断内容,设置实际对应的文字
switch ($state) {
case -1:
$statestr = '已作废';
break;
case 1:
$statestr = '已到仓库';
break;
case 2:
$statestr = '派送中';
break;
case 3:
$statestr = '已完结';
break;
default:
$statestr = '已下单';
break;
}
$order->statestr = $statestr;//新增一列 保存替换后的内容
}
}
x
1
2
3
namespace app\index\model;
4
5
use think\Model;
6
7
class OrderTable extends Model
8
{
9
protected $table = "order";
10
protected $pk = "order_id";
11
12
public static function onAfterRead($order)
13
{
14
$state = $order->state;//得到状态列内容
15
$statestr = '已下单';
16
//判断内容,设置实际对应的文字
17
switch ($state) {
18
case -1:
19
$statestr = '已作废';
20
break;
21
case 1:
22
$statestr = '已到仓库';
23
break;
24
case 2:
25
$statestr = '派送中';
26
break;
27
case 3:
28
$statestr = '已完结';
29
break;
30
31
default:
32
$statestr = '已下单';
33
break;
34
}
35
36
$order->statestr = $statestr;//新增一列 保存替换后的内容
37
}
38
}
39