Skip to content

Commit

Permalink
添加实体模型自动时间字段写入
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Nov 21, 2024
1 parent 5dfaf30 commit 6d5b23d
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 61 deletions.
124 changes: 94 additions & 30 deletions src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,26 @@ public function __construct(array | object $data = [], ?Model $model = null)
}

self::$weakMap[$this] = [
'model' => $model,
'get' => [],
'data' => [],
'schema' => [],
'origin' => [],
'together' => [],
'allow' => [],
'strict_mode' => true,
'type' => $options['type'] ?? [],
'virtual' => $options['virtual'] ?? false,
'readonly' => $options['readonly'] ?? [],
'disuse' => $options['disuse'] ?? [],
'hidden' => $options['hidden'] ?? [],
'visible' => $options['visible'] ?? [],
'append' => $options['append'] ?? [],
'mapping' => $options['mapping'] ?? [],
'strict' => $options['strict'] ?? true,
'relation_keys' => $options['relation_keys'] ?? [],
'model' => $model,
'get' => [],
'data' => [],
'schema' => [],
'origin' => [],
'together' => [],
'allow' => [],
'strict_mode' => true,
'update_time' => $options['update_time'] ?? 'update_time',
'create_time' => $options['create_time'] ?? 'create_time',
'type' => $options['type'] ?? [],
'virtual' => $options['virtual'] ?? false,
'readonly' => $options['readonly'] ?? [],
'disuse' => $options['disuse'] ?? [],
'hidden' => $options['hidden'] ?? [],
'visible' => $options['visible'] ?? [],
'append' => $options['append'] ?? [],
'mapping' => $options['mapping'] ?? [],
'strict' => $options['strict'] ?? true,
'relation_keys' => $options['relation_keys'] ?? [],
];

$model->setEntity($this);
Expand Down Expand Up @@ -112,7 +114,29 @@ protected function parseModel(): string
*/
public function model(): Model
{
return self::$weakMap[$this]['model']->schema(self::$_schema[static::class][0]);
$schema = $this->autoCase(self::$_schema[static::class][0]);
return self::$weakMap[$this]['model']->schema($schema);
}

/**
* 自动转换字段名为小写下划线规范(非严格模式下).
*
* @param array $schema 字段信息
*
* @return array
*/
protected function autoCase(array $schema): array
{
if (!self::$weakMap[$this]['strict']) {
foreach ($schema as $name => $val) {
$trueName = Str::snake($name);
if ($trueName != $name) {
$schema[$trueName] = $val;
unset($schema[$name]);
}
}
}
return $schema;
}

/**
Expand Down Expand Up @@ -554,17 +578,9 @@ public function save(array | object $data = []): bool
return false;
}

if (!self::$weakMap[$this]['strict']) {
// 非严格模式下 自动转换为小写下划线规范
foreach ($data as $name => $val) {
$trueName = Str::snake($name);
if ($trueName != $name) {
$data[$trueName] = $val;
unset($data[$name]);
}
}
}

// 自动时间戳处理
$this->autoDateTime($data, $isUpdate);
$data = $this->autoCase($data);
$result = $this->model()->save($data);

if (false === $result) {
Expand All @@ -579,6 +595,54 @@ public function save(array | object $data = []): bool
return true;
}

/**
* 时间字段自动写入.
*
* @param array $data 数据
* @param bool $update 是否更新
* @return void
*/
protected function autoDateTime(array &$data, bool $update)
{
$dateTimeFields = [self::$weakMap[$this]['update_time']];
if (!$update) {
array_unshift($dateTimeFields, self::$weakMap[$this]['create_time']);
}

foreach ($dateTimeFields as $field) {
if (is_string($field)) {
$data[$field] = $this->getDateTime($field);
$this->$field = $this->readTransform($data[$field], $this->getFields($field));
}
}
}

/**
* 获取当前时间.
*
* @param string $field 字段名
* @return void
*/
protected function getDateTime(string $field)
{
$type = $this->getFields($field);
if ('int' == $type) {
$value = time();
} elseif (is_subclass_of($type, Typeable::class)) {
$value = $type::from('now', $this)->value();
} elseif (str_contains($type, '\\')) {
// 对象数据写入
$obj = new $type();
if ($obj instanceof Stringable) {
// 对象数据写入
$value = $obj->__toString();
}
} else {
$value = \think\model\type\DateTime::from('now', $this)->value();
}
return $value;
}

/**
* 检查字段是否有更新(主键无需更新).
*
Expand Down
52 changes: 27 additions & 25 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ protected function updateData(array $data): bool
return true;
}

if ($this->autoWriteTimestamp && $this->updateTime) {
if (!$this->entity && $this->autoWriteTimestamp && $this->updateTime) {
// 自动写入更新时间
$data[$this->updateTime] = $this->autoWriteTimestamp();
if ($this->entity) {
Expand Down Expand Up @@ -846,35 +846,37 @@ protected function insertData(array $data, ?string $sequence = null): bool
}
}

// 时间字段自动写入
if ($this->autoWriteTimestamp) {
foreach ([$this->createTime, $this->updateTime] as $field) {
if ($field && !array_key_exists($field, $this->data)) {
$data[$field] = $this->autoWriteTimestamp();
if ($this->entity) {
$this->entity->$field = $data[$field];
} else {
$this->data[$field] = $data[$field];
if (!$this->entity) {
// 时间字段自动写入
if ($this->autoWriteTimestamp) {
foreach ([$this->createTime, $this->updateTime] as $field) {
if ($field && !array_key_exists($field, $this->data)) {
$data[$field] = $this->autoWriteTimestamp();
if ($this->entity) {
$this->entity->$field = $data[$field];
} else {
$this->data[$field] = $data[$field];
}
}
}
}
}

// 自动(使用修改器)写入字段
if (!empty($this->insert)) {
foreach ($this->insert as $name => $val) {
$field = is_string($name) ? $name : $val;
if (!isset($data[$field])) {
if (is_string($name)) {
$data[$field] = $val;
$this->data[$field] = $val;
} else {
$this->setAttr($field, null);
$data[$field] = $this->data[$field];
}
// 自动(使用修改器)写入字段
if (!empty($this->insert)) {
foreach ($this->insert as $name => $val) {
$field = is_string($name) ? $name : $val;
if (!isset($data[$field])) {
if (is_string($name)) {
$data[$field] = $val;
$this->data[$field] = $val;
} else {
$this->setAttr($field, null);
$data[$field] = $this->data[$field];
}

if ($this->entity) {
$this->entity->$field = $data[$field];
if ($this->entity) {
$this->entity->$field = $data[$field];
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/model/type/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace think\model\type;

use think\Entity;
use think\model\contract\Modelable;

class Date extends DateTime
{
protected $data;

public static function from(mixed $value, Entity $model)
public static function from(mixed $value, Modelable $model)
{
$static = new static();
$static->data($value, 'Y-m-d');
Expand Down
4 changes: 2 additions & 2 deletions src/model/type/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace think\model\type;

use think\Entity;
use think\model\contract\Modelable;
use think\model\contract\Typeable;

class DateTime implements Typeable
{
protected $data;

public static function from(mixed $value, Entity $model)
public static function from(mixed $value, Modelable $model)
{
$static = new static();
$static->data($value, $model->getDateFormat());
Expand Down
4 changes: 2 additions & 2 deletions src/model/type/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace think\model\type;

use think\Entity;
use think\model\contract\Modelable;
use think\model\contract\Typeable;

class Json implements Typeable
{
protected $data;

public static function from(mixed $value, Entity $model)
public static function from(mixed $value, Modelable $model)
{
$static = new static();
$static->data($value, $model->isJsonAssoc());
Expand Down

0 comments on commit 6d5b23d

Please sign in to comment.