自己扩展了MODEL父类,可以自己判断新增还是修改

浏览:565 发布日期:2017/01/10 分类:系统代码
自己系统用的,感觉还可以,但是个人喜欢精简,发上来看看有没有大神给我精简下,功能又一致的!PS:不太会用行为或者钩子,自己弄了个保存前与保存后的方法,不要见笑
<?php
namespace app;
use think\Model;
use think\Session;
use think\Db;
abstract class BaseModel extends Model
{
    protected $autoWriteTimestamp = true;
    private $autoSave = true;
    protected $eventBefore= true;
    protected $eventAfter= true;

    public function __construct($data=[])
    {
        parent::__construct($data);
        if(in_array('create_time',$this->db()->getTableInfo('', 'fields')))
        {
            $this->createTime = 'create_time';
        }
        else
        {
            $this->createTime = false;
        }
        if(in_array('modify_time',$this->db()->getTableInfo('', 'fields')))
        {
            $this->updateTime = 'modify_time';
        }
        else
        {
            $this->updateTime = false;
        }
        if(in_array('create_by',$this->db()->getTableInfo('', 'fields')) && in_array('modify_by',$this->db()->getTableInfo('', 'fields')))
        {
            $this->insert =['create_by','modify_by'];
        }
        if(in_array('modify_by',$this->db()->getTableInfo('', 'fields')))
        {
            $this->update =['modify_by'];
        }
    }

    public function setCreateByAttr()
    {
        return  Session::get('userInfo.uid');
    }

    public function setModifyByAttr()
    {
        return  Session::get('userInfo.uid');
    }

    public function getCreateTimeAttr($value)
    {
        return date('Y-m-d H:i:s',$value);
    }

    public function getModifyTimeAttr($value)
    {
        return date('Y-m-d H:i:s',$value);
    }

    public function getCreateByAttr($value)
    {
        if(empty($value))
        {
            return "";
        }
        else
        {
            return  Db::name('users')->where('uid',$value)->value('name');
        }
    }

    public function getModifyByAttr($value)
    {
        if(empty($value))
        {
            return "";
        }
        else
        {
            return  Db::name('users')->where('uid',$value)->value('name');
        }
    }

    /**
     * 设置自动保存
     * @param bool $mode
     * @return $this
     */
    public function setAuto($mode = true)
    {
        $this->autoSave = $mode;
        return $this;
    }

    /**
     * 是否执行保存前回调方法
     * @param bool $event
     * @return $this
     */
    public function setEventBefore($event = true)
    {
        $this->eventBefore = $event;
        return $this;
    }

    /**
     * 是否执行保存后回调方法
     * @param bool $event
     * @return $this
     */
    public function setEventAfter($event = true)
    {
        $this->eventAfter = $event;
        return $this;
    }

    /**
     * 是否执行保存回调方法
     * @param bool $event
     * @return $this
     */
    public function setEvent($event = true)
    {
        $this->eventBefore = $this->eventAfter = $event;
        return $this;
    }

    /**
     * 批量保存
     * @param array $dataSet 数据
     * @param bool $replace 是否自动识别更新和写入
     * @return array|bool
     */
    public function saveAll($dataSet=[],$replace = true)
    {
        $this->setAuto($replace);
        $result = [];
        $db = $this->db();
        $db->startTrans();
        try
        {
            if(method_exists($this,'before_all_write') && $this->eventBefore)
            {
                $this->eventBefore = false;
                if(call_user_func_array(array($this, 'before_all_write'),[&$dataSet]) === false)
                {
                    return false;
                }
                else
                {
                    $this->eventBefore = true;
                }
            }
            foreach ($dataSet as $key => $data)
            {
                $result[$key] = $this->save($data);
            }
            $db->commit();
            if(method_exists($this,'after_all_write') && $this->eventAfter)
            {
                $this->eventAfter = false;
                if(call_user_func_array(array($this, 'after_all_write'),[$dataSet]) === false)
                {
                    return false;
                }
                else
                {
                    $this->eventAfter = true;
                }
            }
            return $result;
        }
        catch (\Exception $e)
        {
            $db->rollback();
            return false;
        }
    }

    /**
     * 单笔保存
     * @param array $data 数据
     * @param array $where
     * @param null $sequence
     * @return bool|false|int
     */
    public function save($data = [], $where = [], $sequence = null)
    {
        $this->allowField(true);
        if($this->autoSave)
        {
            $this->isUpdate = $this->hasPk($data);
        }
        if(method_exists($this,'before_write') && $this->eventBefore)
        {
            $this->eventBefore = false;
            if(call_user_func_array(array($this, 'before_write'),[&$data]) === false)
            {
                return false;
            }
            else
            {
                $this->eventBefore = true;
            }
        }
        $status = parent::save($data,$where,$sequence);
        if(method_exists($this,'after_write') && $this->eventAfter)
        {
           $this->eventAfter = false;
           if(call_user_func(array($this, 'after_write')) === false)
           {
               return false;
           }
           else
           {
               $this->eventAfter = true;
           }
        }
        return $status;
    }

    /**
     * 判断是否主键数据
     * @param array $Data
     * @return bool
     */
    public function hasPk($Data=array())
    {
        $pk = $this->getPk();
        if(is_array($pk))
        {
            $find = $this->db()->where(array_intersect_key($Data,array_flip($pk)))->find();
            if(empty($find))
            {
                $this->data([]);
                $flag = false;
            }
            else
            {
                $this->data($find->getData());
                $this->change = $this->getCompleteData($Data);
                $flag = true;
            }
        }
        else{
            if(isset($Data[$pk]))
            {
                $find = $this->get($Data[$pk]);
                if(empty($find))
                {
                    $this->data([]);
                    $flag = false;
                }
                else
                {
                    $this->data($find->getData());
                    $this->change = $this->getCompleteData($Data);
                    $flag = true;
                }
            }
            else
            {
                $this->data([]);
                $flag = false;
            }
        }
        return $flag;
    }

    /**
     * 返回自动条件
     * @param $data
     * @return array
     */
    protected function getCompleteData($data)
    {
        $fields = [];
        foreach (array_merge($this->auto,$this->update) as $field => $value) {
            if (is_integer($field)) {
                if(array_key_exists($value,$data))
                {
                    $fields[] = $value;
                }
                else
                {
                    unset($this->data[$value]);
                }
            }
            else
            {
                if(array_key_exists($field,$data))
                {
                    $fields[] = $field;
                }
                else
                {
                    unset($this->data[$field]);
                }
            }
        }
        return $fields;
    }
}

附件 BaseModel.rar ( 1.65 KB 下载:1 次 )

评论( 相关
后面还有条评论,点击查看>>