<?php

namespace app\admin\model;

use think\Model;


class Gift extends Model
{

    

    

    // 表名
    protected $name = 'gift';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];

    protected static function init()
    {
        //两个毫无用处的字段，没有还不行
        self::afterInsert(function ($row){
            $data = [
                'no' => $row['id'],
                'price' => $row['value'],
            ];

            db('gift')->where('id',$row['id'])->update($data);
        });

        self::beforeUpdate(function ($row) {
            $changed = $row->getChangedData();
            //如果有修改密码
            if (isset($changed['value'])) {
                $row->price = $changed['value'];
            }

        });
    }
    

    







    public function gifttype()
    {
        return $this->belongsTo('Gifttype', 'type', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
