Skip to content

Commit f86f6fa

Browse files
committed
改进模型事件 实体模型增加更新数据自动写入功能
1 parent cdeab3d commit f86f6fa

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

src/Entity.php

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ public function __construct(array | object $data = [], ?Model $model = null)
8585
'mapping' => $options['mapping'] ?? [],
8686
'strict' => $options['strict'] ?? true,
8787
'bind_attr' => $options['bind_attr'] ?? [],
88-
'auto_insert' => $options['auto_insert'] ?? [],
8988
'auto_relation' => $options['auto_relation'] ?? [],
9089
'relation_keys' => $options['relation_keys'] ?? [],
9190
];
@@ -827,10 +826,8 @@ public function save(array | object $data = [], $where = []): bool
827826
// 自动时间戳处理
828827
$this->autoDateTime($data, $isUpdate);
829828

830-
if (!$isUpdate) {
831-
// 自动写入数据
832-
$this->autoInsertData($data);
833-
}
829+
// 自动写入数据
830+
$this->autoWriteData($data, $isUpdate);
834831

835832
$model = $this->model();
836833
if ($model instanceof Model) {
@@ -906,23 +903,22 @@ protected function autoDateTime(array &$data, bool $update)
906903
* 字段自动写入.
907904
*
908905
* @param array $data 数据
906+
* @param bool $isUpdate 是否更新
909907
* @return void
910908
*/
911-
protected function autoInsertData(array &$data)
912-
{
913-
$autoInsert = $this->getOption('auto_insert', []);
914-
if (!empty($autoInsert)) {
915-
foreach ($autoInsert as $name => $val) {
916-
$field = is_string($name) ? $name : $val;
917-
if (!isset($data[$field])) {
918-
if ($val instanceof Closure) {
919-
$value = $val($this);
920-
} else {
921-
$value = is_string($name) ? $val : $this->setWithAttr($field, null, $data);
922-
}
923-
$data[$field] = $value;
924-
$this->setData($field, $value);
909+
protected function autoWriteData(array &$data, bool $isUpdate)
910+
{
911+
$auto = $this->getOption($isUpdate ? 'auto_update' : 'auto_insert', []);
912+
foreach ($auto as $name => $val) {
913+
$field = is_string($name) ? $name : $val;
914+
if (!isset($data[$field])) {
915+
if ($val instanceof Closure) {
916+
$value = $val($this);
917+
} else {
918+
$value = is_string($name) ? $val : $this->setWithAttr($field, null, $data);
925919
}
920+
$data[$field] = $value;
921+
$this->setData($field, $value);
926922
}
927923
}
928924
}

src/Model.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ protected function updateData(array $data): bool
762762

763763
// 获取有更新的数据
764764
if (!$this->entity) {
765-
$data = $this->getChangedData($data);
765+
$data = $this->getChangedData($this->data);
766766
}
767767

768768
if (empty($data)) {
@@ -841,6 +841,10 @@ protected function insertData(array $data, ?string $sequence = null): bool
841841

842842
$this->checkData();
843843

844+
if (!$this->entity) {
845+
$data = $this->data;
846+
}
847+
844848
// 主键自动写入
845849
if ($this->isAutoWriteId()) {
846850
$pk = $this->getPk();

0 commit comments

Comments
 (0)