OrderGoods.php 1.6 KB
<?php

namespace app\common\model;

use think\Model;

class OrderGoods extends Model
{
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    // 追加属性
    protected $append = [
        'goods_style_text'
    ];

    /**
     * 商品
     */
    public function goods()
    {
        return $this->belongsTo('Goods');
    }

    /**
     * 关联商品规格表
     */
    public function spec()
    {
        return $this->belongsTo('GoodsSpec', 'spec_sku_id', 'spec_sku_id');
    }
    
    /**
     * 定制项详情
     */
    public function getGoodsStyleTextAttr($value,$data){
        $goods_style_text = [];
        if(!empty($data['goods_style'])){
            $goods_style = json_decode(str_replace('\'','"',$data['goods_style']),true);
            foreach($goods_style as $k => $v){
                $style = Style::where('id',$k)->field('style_name,style_type')->find()->toArray();
                $style['style_value'] = '';
                switch ($style['style_type']) {
                	case '1':
                		$style['style_value'] = StyleValue::where('id',$v)->value('style_value_name');
                		break;
                	case '2':
                		$style['style_value'] = $v;
                		break;
                	case '3':
                		$style['style_value'] = cdnurl($v,true);
                		break;
                }
                $goods_style_text[] = $style;
            }
        }
        return $goods_style_text;
    }
}