diff --git a/src/Entity.php b/src/Entity.php index e0582095..5c949cd2 100644 --- a/src/Entity.php +++ b/src/Entity.php @@ -15,6 +15,7 @@ use ArrayAccess; use BackedEnum; +use Closure; use InvalidArgumentException; use JsonSerializable; use Stringable; @@ -84,6 +85,7 @@ public function __construct(array | object $data = [], ?Model $model = null) 'mapping' => $options['mapping'] ?? [], 'strict' => $options['strict'] ?? true, 'bind_attr' => $options['bind_attr'] ?? [], + 'auto_insert' => $options['auto_insert'] ?? [], 'auto_relation' => $options['auto_relation'] ?? [], 'relation_keys' => $options['relation_keys'] ?? [], ]; @@ -757,6 +759,11 @@ public function save(array | object $data = [], $where = []): bool // 自动时间戳处理 $this->autoDateTime($data, $isUpdate); + if (!$isUpdate) { + // 自动写入数据 + $this->autoInsertData($data); + } + $model = $this->model(); if ($model instanceof Model) { $result = $model->allowField($allow)->setUpdateWhere($where)->save($data); @@ -805,6 +812,30 @@ protected function autoDateTime(array &$data, bool $update) } } + /** + * 字段自动写入. + * + * @param array $data 数据 + * @return void + */ + protected function autoInsertData(array &$data) + { + if (!empty(self::$weakMap[$this]['auto_insert'])) { + foreach (self::$weakMap[$this]['auto_insert'] as $name => $val) { + $field = is_string($name) ? $name : $val; + if (!isset($data[$field])) { + if ($val instanceof Closure) { + $value = $val($this); + } else { + $value = is_string($name) ? $val : $this->setWithAttr($field, null, $data); + } + $data[$field] = $value; + $this->setData($field, $value); + } + } + } + } + /** * 获取当前时间. *