1234567891011121314151617181920212223242526272829303132333435363738394041 |
- <?php
- namespace app\common\model;
- use think\Model;
- class UserTax extends Model
- {
- protected $autoWriteTimestamp=true;
- protected $updateTime=null;
- protected $type=[
- 'tax'=>'json',
- ];
- public static function fromOrder(Orders $orders){
- if($orders['tax']){
- $has=self::where('user_id',$orders['user_id'])->where('tax',json_encode($orders['tax'],JSON_UNESCAPED_UNICODE))->find();
- if(!$has) {
- self::create([
- 'user_id' => $orders['user_id'],
- 'tax' => $orders['tax'],
- 'order_id'=>$orders->id,
- ]);
- }
- }
- }
- public function orders(){
- return $this->belongsTo(Orders::class,'order_id');
- }
- protected static function init()
- {
- self::afterWrite(function (self $tax){
- if(!empty($tax['is_default'])){
- self::where('user_id',$tax['user_id'])->where('id','<>',$tax['id'])->update(['is_default'=>0]);
- }
- });
- }
- }
|