来由:
数据库的这个字段我想返回成:
新奇的写法如下:
逻辑层的代码:
public function goodsDetail($goodId)
{
$detail = $this->good->where('id', $goodId)->hidden(['type_params','user_id'])->find();
if (!$detail) {
return $this->fail("获取数据失败!");
}
if ($detail->type === 1) {
//特殊的写法
$detail->award_lists = '';
} else if ($detail->type === 2) {
$number_group_buyer = Order::where('good_id', $goodId)->where('status',1)->count('id');
$detail->number_group_buyers = $number_group_buyer;
}
return $this->success("获取数据成功", $detail);
}
模型层的代码:
<?php
namespace app\daogou\model;
use app\common\model\TimeModel;
class Goods extends TimeModel
{
protected $table = 'dg_goods';
protected $type = [
'type_params'=>'json',
];
//type_params:
// type = 1 : [['coin_id'=>1,'award'=>"0.80000000"],……]
// type = 2 : ['max_number'=>10,'winner'=>1]
// type = 3 : null
public function user()
{
return $this->hasOne('user','user_id','id')->bind(['username']);
}
public function getStatusTxtAttr($v,$data)
{
//状态(-1:下架,1:上架,2:交易中)
$status = [-1=>"下架",1=>"上架",2=>"交易中"];
if(array_key_exists('status',$data)){
return array_key_exists($data['status'],$status) ? $status[$data['status']] : $data['status'];
}
return '';
}
public function getPriceAttr($v)
{
return delZero($v);
}
public function getTypeTxtAttr($v,$data){
//(1:奖购2:团购,3:闪购)
$status = [1=>"奖购",2=>"团购",3=>"闪购"];
if(array_key_exists('type',$data)){
return array_key_exists($data['type'],$status) ? $status[$data['type']] : $data['type'];
}
return '';
}
// 不理解的代码
public function getAwardListsAttr($v, $good)
{
$awardLists = [];
if ($good['type'] === 1) {
$type_params = json_decode($good['type_params'],true);
foreach ($type_params as $propertyId => $properNumber) {
$propertyName = Coin::where('id', $propertyId)->value('name');
$award['asset_name'] = $propertyName;
$award['asset_number'] = $properNumber;
array_push($awardLists,$award);
}
}
return $awardLists;
}
}
可以通过 :
$ba = debug_backtrace(); var_dump($ba); die();
来查看执行的流程,,流程如下(个人感觉看起来 【展示的效果】 不是很友好 很乱的感觉 ):
array(8) {
[0]=>
array(7) {
["file"]=>
string(66) "D:\wwwroot\yyex\thinkphp\library\think\model\concern\Attribute.php"
["line"]=>
int(500)
["function"]=>
string(17) "getAwardListsAttr"
["class"]=>
string(22) "app\daogou\model\Goods"
["object"]=>
object(app\daogou\model\Goods)#58 (2) {
["data"]=>
array(14) {
["id"]=>
int(14)
["user_id"]=>
int(2)
["type"]=>
int(1)
["name"]=>
string(14) "仙侠小说ss"
["photo"]=>
string(20) "http://ff12.ffsky.cn"
["price"]=>
string(6) "102.30"
["introduction"]=>
string(5) "ccsss"
["phone"]=>
string(11) "13246885231"
["type_params"]=>
string(9) "{"20":22}"
["status"]=>
int(1)
["create_time"]=>
int(1701844614)
["update_time"]=>
int(1701844614)
["remark"]=>
NULL
["award_lists"]=>
string(0) ""
}
["relation"]=>
array(0) {
}
}
["type"]=>
string(2) "->"
["args"]=>
array(2) {
[0]=>
string(0) ""
[1]=>
array(14) {
["id"]=>
int(14)
["user_id"]=>
int(2)
["type"]=>
int(1)
["name"]=>
string(14) "仙侠小说ss"
["photo"]=>
string(20) "http://ff12.ffsky.cn"
["price"]=>
string(6) "102.30"
["introduction"]=>
string(5) "ccsss"
["phone"]=>
string(11) "13246885231"
["type_params"]=>
string(9) "{"20":22}"
["status"]=>
int(1)
["create_time"]=>
int(1701844614)
["update_time"]=>
int(1701844614)
["remark"]=>
NULL
["award_lists"]=>
string(0) ""
}
}
}
[1]=>
array(7) {
["file"]=>
string(67) "D:\wwwroot\yyex\thinkphp\library\think\model\concern\Conversion.php"
["line"]=>
int(179)
["function"]=>
string(7) "getAttr"
["class"]=>
string(11) "think\Model"
["object"]=>
object(app\daogou\model\Goods)#58 (2) {
["data"]=>
array(14) {
["id"]=>
int(14)
["user_id"]=>
int(2)
["type"]=>
int(1)
["name"]=>
string(14) "仙侠小说ss"
["photo"]=>
string(20) "http://ff12.ffsky.cn"
["price"]=>
string(6) "102.30"
["introduction"]=>
string(5) "ccsss"
["phone"]=>
string(11) "13246885231"
["type_params"]=>
string(9) "{"20":22}"
["status"]=>
int(1)
["create_time"]=>
int(1701844614)
["update_time"]=>
int(1701844614)
["remark"]=>
NULL
["award_lists"]=>
string(0) ""
}
["relation"]=>
array(0) {
}
}
["type"]=>
string(2) "->"
["args"]=>
array(1) {
[0]=>
string(11) "award_lists"
}
}
[2]=>
array(7) {
["file"]=>
string(67) "D:\wwwroot\yyex\thinkphp\library\think\model\concern\Conversion.php"
["line"]=>
int(250)
["function"]=>
string(7) "toArray"
["class"]=>
string(11) "think\Model"
["object"]=>
object(app\daogou\model\Goods)#58 (2) {
["data"]=>
array(14) {
["id"]=>
int(14)
["user_id"]=>
int(2)
["type"]=>
int(1)
["name"]=>
string(14) "仙侠小说ss"
["photo"]=>
string(20) "http://ff12.ffsky.cn"
["price"]=>
string(6) "102.30"
["introduction"]=>
string(5) "ccsss"
["phone"]=>
string(11) "13246885231"
["type_params"]=>
string(9) "{"20":22}"
["status"]=>
int(1)
["create_time"]=>
int(1701844614)
["update_time"]=>
int(1701844614)
["remark"]=>
NULL
["award_lists"]=>
string(0) ""
}
["relation"]=>
array(0) {
}
}
["type"]=>
string(2) "->"
["args"]=>
array(0) {
}
}
[3]=>
array(5) {
["function"]=>
string(13) "jsonSerialize"
["class"]=>
string(11) "think\Model"
["object"]=>
object(app\daogou\model\Goods)#58 (2) {
["data"]=>
array(14) {
["id"]=>
int(14)
["user_id"]=>
int(2)
["type"]=>
int(1)
["name"]=>
string(14) "仙侠小说ss"
["photo"]=>
string(20) "http://ff12.ffsky.cn"
["price"]=>
string(6) "102.30"
["introduction"]=>
string(5) "ccsss"
["phone"]=>
string(11) "13246885231"
["type_params"]=>
string(9) "{"20":22}"
["status"]=>
int(1)
["create_time"]=>
int(1701844614)
["update_time"]=>
int(1701844614)
["remark"]=>
NULL
["award_lists"]=>
string(0) ""
}
["relation"]=>
array(0) {
}
}
["type"]=>
string(2) "->"
["args"]=>
array(0) {
}
}
[4]=>
array(4) {
["file"]=>
string(56) "D:\wwwroot\yyex\thinkphp\library\think\response\Json.php"
["line"]=>
int(36)
["function"]=>
string(11) "json_encode"
["args"]=>
array(2) {
[0]=>
*RECURSION*
[1]=>
int(256)
}
}
[5]=>
array(7) {
["file"]=>
string(51) "D:\wwwroot\yyex\thinkphp\library\think\Response.php"
["line"]=>
int(396)
["function"]=>
string(6) "output"
["class"]=>
string(19) "think\response\Json"
["object"]=>
object(think\response\Json)#57 (8) {
["options"]=>
array(1) {
["json_encode_param"]=>
int(256)
}
["contentType"]=>
string(16) "application/json"
["data"]=>
*RECURSION*
["charset"]=>
string(5) "utf-8"
["code"]=>
int(200)
["allowCache"]=>
bool(true)
["header"]=>
array(1) {
["Content-Type"]=>
string(31) "application/json; charset=utf-8"
}
["content"]=>
NULL
}
["type"]=>
string(2) "->"
["args"]=>
array(1) {
[0]=>
*RECURSION*
}
}
[6]=>
array(7) {
["file"]=>
string(51) "D:\wwwroot\yyex\thinkphp\library\think\Response.php"
["line"]=>
int(128)
["function"]=>
string(10) "getContent"
["class"]=>
string(14) "think\Response"
["object"]=>
object(think\response\Json)#57 (8) {
["options"]=>
array(1) {
["json_encode_param"]=>
int(256)
}
["contentType"]=>
string(16) "application/json"
["data"]=>
*RECURSION*
["charset"]=>
string(5) "utf-8"
["code"]=>
int(200)
["allowCache"]=>
bool(true)
["header"]=>
array(1) {
["Content-Type"]=>
string(31) "application/json; charset=utf-8"
}
["content"]=>
NULL
}
["type"]=>
string(2) "->"
["args"]=>
array(0) {
}
}
[7]=>
array(7) {
["file"]=>
string(32) "D:\wwwroot\yyex\public\index.php"
["line"]=>
int(18)
["function"]=>
string(4) "send"
["class"]=>
string(14) "think\Response"
["object"]=>
object(think\response\Json)#57 (8) {
["options"]=>
array(1) {
["json_encode_param"]=>
int(256)
}
["contentType"]=>
string(16) "application/json"
["data"]=>
*RECURSION*
["charset"]=>
string(5) "utf-8"
["code"]=>
int(200)
["allowCache"]=>
bool(true)
["header"]=>
array(1) {
["Content-Type"]=>
string(31) "application/json; charset=utf-8"
}
["content"]=>
NULL
}
["type"]=>
string(2) "->"
["args"]=>
array(0) {
}
}
}