mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-08 07:32:48 +08:00
Model类增加自动记录时间戳字段功能 改进数据自动完成
This commit is contained in:
@@ -48,14 +48,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
protected $validate;
|
protected $validate;
|
||||||
// 字段完成规则
|
// 字段完成规则
|
||||||
protected $auto = [];
|
protected $auto = [];
|
||||||
|
|
||||||
|
// 自动时间戳记录
|
||||||
|
protected $timestamps = true;
|
||||||
|
// 新增时间戳字段
|
||||||
|
protected $createTimeField = 'create_time';
|
||||||
|
// 更新时间戳字段
|
||||||
|
protected $updateTimeField = 'update_time';
|
||||||
|
|
||||||
// 新增的字段完成
|
// 新增的字段完成
|
||||||
protected $insert = [
|
protected $insert = [];
|
||||||
'create_time' => 'time',
|
|
||||||
];
|
|
||||||
// 更新的字段完成
|
// 更新的字段完成
|
||||||
protected $update = [
|
protected $update = [];
|
||||||
'update_time' => 'time',
|
|
||||||
];
|
|
||||||
|
|
||||||
// 字段类型或者格式转换
|
// 字段类型或者格式转换
|
||||||
protected $type = [];
|
protected $type = [];
|
||||||
@@ -169,10 +173,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
case 'boolean':
|
case 'boolean':
|
||||||
$value = (bool) $value;
|
$value = (bool) $value;
|
||||||
break;
|
break;
|
||||||
default:
|
|
||||||
if (is_callable($type)) {
|
|
||||||
$value = call_user_func_array($type, [$value]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -207,10 +207,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
case 'boolean':
|
case 'boolean':
|
||||||
$value = (bool) $value;
|
$value = (bool) $value;
|
||||||
break;
|
break;
|
||||||
default:
|
|
||||||
if (is_callable($type)) {
|
|
||||||
$value = call_user_func_array($type, [$value]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -318,10 +314,13 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
foreach ($this->auto as $name => $rule) {
|
foreach ($this->auto as $name => $rule) {
|
||||||
if (!in_array($name, $this->change)) {
|
if (!in_array($name, $this->change)) {
|
||||||
$this->change[] = $name;
|
$this->change[] = $name;
|
||||||
|
$data[$name] = $this->auto($name, $rule, $data);
|
||||||
}
|
}
|
||||||
$data[$name] = is_callable($rule) ? call_user_func_array($rule, [ & $data]) : $rule;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($this->timestamps) {
|
||||||
|
$data[$this->updateTimeField] = NOW_TIME;
|
||||||
|
}
|
||||||
// 检测是否为更新数据
|
// 检测是否为更新数据
|
||||||
if ($this->isUpdate($data)) {
|
if ($this->isUpdate($data)) {
|
||||||
|
|
||||||
@@ -337,7 +336,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
|
|
||||||
// 自动更新
|
// 自动更新
|
||||||
foreach ($this->update as $name => $rule) {
|
foreach ($this->update as $name => $rule) {
|
||||||
$data[$name] = is_callable($rule) ? call_user_func_array($rule, [ & $data]) : $rule;
|
$data[$name] = $this->auto($name, $rule, $data);
|
||||||
}
|
}
|
||||||
|
|
||||||
$db = self::db();
|
$db = self::db();
|
||||||
@@ -357,9 +356,13 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($this->timestamps) {
|
||||||
|
$data[$this->createTimeField] = NOW_TIME;
|
||||||
|
}
|
||||||
|
|
||||||
// 自动写入
|
// 自动写入
|
||||||
foreach ($this->insert as $name => $rule) {
|
foreach ($this->insert as $name => $rule) {
|
||||||
$data[$name] = is_callable($rule) ? call_user_func_array($rule, [ & $data]) : $rule;
|
$data[$name] = $this->auto($name, $rule, $data);
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = self::db()->insert($data);
|
$result = self::db()->insert($data);
|
||||||
@@ -385,6 +388,28 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据自动完成
|
||||||
|
* @access protected
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
protected function auto($key, $val, &$data)
|
||||||
|
{
|
||||||
|
$value = isset($data[$key]) ? $data[$key] : null;
|
||||||
|
$rule = isset($val[0]) ? $val[0] : $val;
|
||||||
|
$type = isset($val[1]) ? $val[1] : 'value';
|
||||||
|
switch ($type) {
|
||||||
|
case 'callback':
|
||||||
|
$result = call_user_func_array($rule, [$value, &$data]);
|
||||||
|
break;
|
||||||
|
case 'value':
|
||||||
|
default:
|
||||||
|
$result = $rule;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除当前的记录
|
* 删除当前的记录
|
||||||
* @access public
|
* @access public
|
||||||
|
|||||||
Reference in New Issue
Block a user