<?php
namespace app\lib\model;

use think\Model;

class Dszh extends Model
{
    // 设置当前模型对应的主键名
    protected $pk = 'id';

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 只读字段
    protected $readonly = ['user_id', 'username'];

    // 一对一关联 Order
    public function OrderHasOne() {
        return $this->hasOne(Order::class);
    }

    // 一对多关联 Order
    public function OrderHasMany()
    {
        return $this->hasMany(Order::class);
    }

    // 一对一关联 Pay
    public function PayHasOne() {
        return $this->hasOne(Pay::class);
    }

    // 一对多关联 Pay
    public function PayHasMany()
    {
        return $this->hasMany(Pay::class);
    }

    // 一对一关联 Money
    public function MoneyHasOne() {
        return $this->hasOne(Money::class);
    }

    // 一对多关联 Money
    public function MoneyHasMany()
    {
        return $this->hasMany(Money::class);
    }

    // 一对一关联 Chozhi
    public function ChozhiHasOne() {
        return $this->hasOne(Chozhi::class);
    }

    // 一对多关联 Chozhi
    public function ChozhiHasMany()
    {
        return $this->hasMany(Chozhi::class);
    }

    //删除后
    protected static function onAfterDelete($data)
    {
        try {
            Order::where('dszh_id',$data['id'])->delete();
            Money::where('dszh_id',$data['id'])->delete();
            Chozhi::where('dszh_id',$data['id'])->delete();
        } catch (\Exception $e) {
            trace($e->getMessage(), 'error');
        }
    }
}