thinkphp lists todo

来由:

数据库的这个字段我想返回成:

新奇的写法如下:

逻辑层的代码:

  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) {
    }
  }
}
复制代码
2:异步消息队列:
复制代码
tp6+异步任务队列+发送邮件+模型监听_tp6异步下载_Upordo的博客-CSDN博客

3:thinkphp6 + workman 实现即使聊天 通讯:

https://www.cnblogs.com/codehaoran/p/16817231.html

PHP聊天室框架workerman-chat

GitHub - walkor/workerman-chat: Websocket chat room written in PHP based on workerman.

前端聊天模板:lemon imui(gitee)

thinkphp :

复制代码
think PHP libary  Bootstrap类型
相关推荐
Mr.Liu630 分钟前
小程序24-滚动效果:scroll-view组件详解
前端·微信小程序·小程序
fanxiaohui1213834 分钟前
浪潮信息自动驾驶框架AutoDRRT 2.0,赋能高阶自动驾驶
运维·服务器·网络·人工智能·机器学习·金融·自动驾驶
三金121381 小时前
局部使用Vue
前端·javascript·vue.js
LinXunFeng1 小时前
Flutter - 子部件任意位置观察滚动数据
前端·flutter·开源
好开心331 小时前
js高级06-ajax封装和跨域
开发语言·前端·javascript·ajax·okhttp·ecmascript·交互
小镇程序员1 小时前
vue2 src_Todolist消息订阅版本
前端·javascript·vue.js
EEE1even1 小时前
Linux服务器驱动安装
linux·运维·服务器
Zack No Bug1 小时前
解决报错:rror: error:0308010C:digital envelope routines::unsupported
前端·javascript·vue.js
九幽归墟1 小时前
深入理解 CPU 和 GPU 渲染
前端·浏览器·gpu
我们的五年2 小时前
【MySQL课程学习】:MySQL安装,MySQL如何登录和退出?MySQL的简单配置
linux·服务器·数据库·学习·mysql·adb