<?php
declare (strict_types = 1);

namespace app\index\model;

use app\common\model\ProductSkuBaseModel;

/**
 * 产品SKU模型
 * @mixin \think\Model
 */
class ProductSkuModel extends ProductSkuBaseModel
{
    // json字段
    protected $json = ['photo_album'];
    protected $jsonAssoc = true;

    // 关联sku属性
    public function attr()
    {
        return $this->hasMany(ProductSkuAttrModel::class, 'sku_id', 'id');
    }

    // 所属产品范围查询
    public function scopeByProductId($query, $product)
    {
        if (is_array($product)) {
            $query->whereIn('product_id', $product);
            return;
        }
        $query->where('product_id', '=', $product);
    }
}
