tp6 模型关联,添加不存在字段,显示字段,获取器用法

//获取器,重新赋值藏品售卖状态---前台
    public function getSaleTypeAttr($value,$data){
        if ($data['num']<=$data['sale_num']){
            $value = '已售罄';
        }else{
            $value = '售卖中';
        }
        return $value;
    }

    //查询藏品--前台
    public function apiAllColl($pag,$pageSize){
        $res =$this->withJoin(['issuer','category'])
            ->where('collection.status',1)
            ->where('collection.type',1)
            ->where('collection.start_buy','<=',time())
            ->where('collection.start_buy','<>',0)
            ->order('collection.id','desc')
            ->page($pag,$pageSize)
            ->select()
            ->append(['sale_type'])
            ->visible(['id','plan_img','name','cre_name','num','faxing_name','price','like_num'])
            ->hidden(['issuer','category'])
            ->toArray();
        $total = $this->withJoin(['issuer','category'])
            ->where('collection.status',1)
            ->where('collection.type',1)
            ->where('collection.start_buy','<=',time())
            ->where('collection.start_buy','<>',0)
            ->order('collection.id','desc')
            ->count();
        $res_arr['total'] = $total;
        $res_arr['per_page'] = $pageSize;
        $res_arr['current_page'] = $pag;
        $res_arr['last_page'] = ceil($total/$pageSize);
        $res_arr['data'] = $res;
        return $res_arr;
    }

//关联分类表
    public function category()
    {
        return $this->hasOne(Category::class,'id','cate_id')->bind(['cre_name'=>'name']);
    }

    //关联发行方
    public function issuer(){
        return $this->hasOne(Issuer::class,'id','issue_id')->bind(['faxing_name'=>'name']);
    }
<?php
declare (strict_types = 1);

namespace app\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class Collection extends Model
{
    protected $table = 'wy_collection';
    protected $pk = 'id';
    //设置自动类型
    protected $type = [
        'start_buy'=>'timestamp',
    ];

    //查询所有数据
    public function couColl(){
        return $this->count();
    }

    //查询藏品--后台
    public function allColl($pag,$pageSize,$where){
        $neCo = $this->withJoin(['category','issuer'])
            ->where('wy_collection.name','like', '%'.$where.'%')
            ->paginate(['list_rows' => $pageSize, 'page' => $pag])
            ->toArray();
        return $neCo;
    }

    //修改藏品
    public function updColl($param){
        $sql = $this->findOrEmpty($param['id']);
        if ($sql->isEmpty()){
            return false;
        }
        if ($sql['sale_num']>0){
            return false;
        }
        $sql = $sql->save($param);
        return $sql;
    }

    //关联分类表
    public function category()
    {
        return $this->hasOne(Category::class,'id','cate_id')->bind(['cre_name'=>'name']);
    }

    //关联发行方
    public function issuer(){
        return $this->hasOne(Issuer::class,'id','issue_id')->bind(['faxing_name'=>'name']);
    }

    //获取器,重新赋值藏品售卖状态---前台
    public function getSaleTypeAttr($value,$data){
        if ($data['num']<=$data['sale_num']){
            $value = '已售罄';
        }else{
            $value = '售卖中';
        }
        return $value;
    }

   //查询藏品--前台
    public function apiAllColl($pag,$pageSize){
        $res =$this->withJoin(['issuer','category'])
            ->where('collection.status',1)
            ->where('collection.type',1)
            ->where('collection.start_buy','<=',time())
            ->where('collection.start_buy','<>',0)
            ->order('collection.id','desc')
            ->page($pag,$pageSize)
            ->select()
            ->append(['sale_type'])
            ->visible(['id','plan_img','name','cre_name','num','faxing_name','price','like_num'])
            ->hidden(['issuer','category'])
            ->toArray();
        $total = $this->withJoin(['issuer','category'])
            ->where('collection.status',1)
            ->where('collection.type',1)
            ->where('collection.start_buy','<=',time())
            ->where('collection.start_buy','<>',0)
            ->order('collection.id','desc')
            ->count();
        $res_arr['total'] = $total;
        $res_arr['per_page'] = $pageSize;
        $res_arr['current_page'] = $pag;
        $res_arr['last_page'] = ceil($total/$pageSize);
        $res_arr['data'] = $res;
        return $res_arr;
    }
use GuzzleHttp\Client;
/**
     *  请求方法
     */
    public function apiRequest($api, $data){
        $headers = ["Content-Type" => "application/json","X-HTTP-Method-Override" => "post","Content-Length"  => strlen(json_encode($data))];
        $client = new Client();
        $response = $client->request('post', $api,['headers' => $headers,'body'=>json_encode($data)]);
        $result = json_decode($response->getBody()->getContents(),true);
        return $result;
    }

composer

"require": {
        "php": ">=7.2.5",
        "topthink/framework": "^6.0.0",
        "topthink/think-orm": "^2.0",
        "topthink/think-multi-app": "^1.0",
        "topthink/think-captcha": "^3.0",
        "guzzlehttp/guzzle": "^7.4",
        "firebase/php-jwt": "^6.2",
      "ext-json": "*",
        "topthink/think-annotation": "1.1.3",
        "topthink/think-view": "^1.0",
        "aliyuncs/oss-sdk-php": "^2.5",
        "simplito/elliptic-php": "^1.0",
        "dh2y/think-qrcode": "^2.0",
        "topthink/think-queue": "^3.0"
    },
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值