搜索
系统检测到您的用户名不符合规范:

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

浏览:1349 发布日期: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 次 )

收藏
season886
积分:5926 等级:LV4
热点推荐
(追記) (追記ここまで)
最新更新

我们

合作

网站

信息

ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。

AltStyle によって変換されたページ (->オリジナル) /