From 8dca158d389f19ccea61546f0fe7a7ba5110ff83 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 19 Nov 2024 13:57:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E4=BD=93=E6=A8=A1=E5=9E=8B=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0hidden/visible/append=E6=96=B9=E6=B3=95=20=E5=8F=8A?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E6=98=A0=E5=B0=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Entity.php | 143 +++++++++++++++++++++++++++++--- src/model/concern/TimeStamp.php | 5 -- 2 files changed, 131 insertions(+), 17 deletions(-) diff --git a/src/Entity.php b/src/Entity.php index 95f3fec9..e0b4b758 100644 --- a/src/Entity.php +++ b/src/Entity.php @@ -61,6 +61,10 @@ public function __construct(array | object $data = [], ?Model $model = null) 'origin' => [], 'schema' => [], 'together' => [], + 'hidden' => [], + 'visible' => [], + 'append' => [], + 'mapping' => [], 'strict' => true, 'model' => $model, ]; @@ -108,6 +112,12 @@ protected function initializeData(array | object $data, bool $fromSave = false) // 实体模型赋值 foreach ($data as $name => $val) { + if (!empty(self::$weakMap[$this]['mapping'])) { + $key = array_search($name, self::$weakMap[$this]['mapping']); + if (is_string($key)) { + $name = $key; + } + } $trueName = $this->getRealFieldName($name); if (in_array($trueName, $fields)) { // 读取数据后进行类型转换 @@ -270,7 +280,7 @@ protected function getFields(?string $field = null) $schema = []; foreach ($propertys as $property) { - $name = $weakMap['model']->getRealFieldName($property->getName()); + $name = $this->getRealFieldName($property->getName()); $type = $property->hasType() ? $property->getType()->getName() : 'string'; $schema[$name] = $type; } @@ -325,6 +335,93 @@ public function together(array $relation) return $this; } + /** + * 强制写入或删除 + * + * @param bool $force 强制更新 + * + * @return $this + */ + public function force(bool $force = true) + { + $this->model()->force($force); + + return $this; + } + + /** + * 新增数据是否使用Replace. + * + * @param bool $replace + * + * @return $this + */ + public function replace(bool $replace = true) + { + $this->model()->replace($replace); + + return $this; + } + + /** + * 设置需要附加的输出属性. + * + * @param array $append 属性列表 + * @param bool $merge 是否合并 + * + * @return $this + */ + public function append(array $append, bool $merge = false) + { + self::$weakMap[$this]['append'] = $merge ? array_merge(self::$weakMap[$this]['append'], $append) : $append; + + return $this; + } + + /** + * 设置需要隐藏的输出属性. + * + * @param array $hidden 属性列表 + * @param bool $merge 是否合并 + * + * @return $this + */ + public function hidden(array $hidden, bool $merge = false) + { + self::$weakMap[$this]['hidden'] = $merge ? array_merge(self::$weakMap[$this]['hidden'], $hidden) : $hidden; + + return $this; + } + + /** + * 设置需要输出的属性. + * + * @param array $visible + * @param bool $merge 是否合并 + * + * @return $this + */ + public function visible(array $visible, bool $merge = false) + { + self::$weakMap[$this]['visible'] = $merge ? array_merge(self::$weakMap[$this]['visible'], $visible) : $visible; + + return $this; + } + + /** + * 设置属性的映射输出. + * + * @param array $map + * + * @return $this + */ + public function mapping(array $map) + { + self::$weakMap[$this]['mapping'] = $map; + + return $this; + } + /** * 字段值增长 * @@ -372,7 +469,7 @@ public function save(array | object $data = []): bool $data = $this->getData(); $origin = $this->getOrigin(); - $isUpdate = $this->model()->getKey(); + $isUpdate = $this->model()->getKey() && !$this->model()->isForce(); foreach ($data as $name => &$val) { if ($val instanceof Entity) { @@ -505,10 +602,11 @@ public static function create(array | object $data): Entity * 删除记录. * * @param mixed $data 主键列表 支持闭包查询条件 + * @param bool $force 是否强制删除 * * @return bool */ - public static function destroy($data): bool + public static function destroy($data, bool $force = false): bool { if (empty($data) && 0 !== $data) { return false; @@ -532,7 +630,7 @@ public static function destroy($data): bool $resultSet = $query->select((array) $data); foreach ($resultSet as $result) { - $result->delete(); + $result->force($force)->delete(); } return true; @@ -594,6 +692,12 @@ public function getOrigin(): array public function toArray(array $allow = []): array { $data = $this->getData(); + if (empty($allow)) { + $hidden = self::$weakMap[$this]['hidden']; + $visible = self::$weakMap[$this]['visible']; + $allow = array_diff($visible ?: array_keys($data), $hidden); + } + foreach ($data as $name => &$item) { if (!empty($allow) && !in_array($name, $allow)) { unset($data[$name]); @@ -613,18 +717,20 @@ public function toArray(array $allow = []): array $this->setData('get', $name, $item); } } - } - // 输出额外属性 - foreach ($this->getWeakData('get') as $name => $val) { - if (!empty($allow) && !in_array($name, $allow)) { - continue; + if (isset(self::$weakMap[$this]['mapping'][$name])) { + // 检查字段映射 + $key = self::$weakMap[$this]['mapping'][$name]; + $data[$key] = $data[$name]; + unset($data[$name]); } + } - if (!array_key_exists($name, $data)) { - $data[$name] = $val; - } + // 输出额外属性 + foreach (self::$weakMap[$this]['append'] as $key) { + $data[$key] = $this->get($key); } + return $data; } @@ -648,6 +754,12 @@ public function isEmpty(): bool */ public function set(string $name, $value): void { + if (!empty(self::$weakMap[$this]['mapping'])) { + $key = array_search($name, self::$weakMap[$this]['mapping']); + if (is_string($key)) { + $name = $key; + } + } $name = $this->getRealFieldName($name); if ($this->isStrictMode()) { $this->$name = $value; @@ -665,6 +777,11 @@ public function set(string $name, $value): void */ public function get(string $name) { + if (isset(self::$weakMap[$this]['mapping'][$name])) { + // 检查字段映射 + $name = self::$weakMap[$this]['mapping'][$name]; + } + $name = $this->getRealFieldName($name); if (array_key_exists($name, self::$weakMap[$this]['get'])) { return self::$weakMap[$this]['get'][$name]; @@ -763,10 +880,12 @@ public function __debugInfo() if (!$this->isStrictMode()) { return [ 'data' => self::$weakMap[$this]['data'], + 'origin' => self::$weakMap[$this]['origin'], 'schema' => self::$weakMap[$this]['schema'], ]; } else { return [ + 'origin' => self::$weakMap[$this]['origin'], 'schema' => self::$weakMap[$this]['schema'], ]; } diff --git a/src/model/concern/TimeStamp.php b/src/model/concern/TimeStamp.php index 71dd485d..b83b30bf 100644 --- a/src/model/concern/TimeStamp.php +++ b/src/model/concern/TimeStamp.php @@ -64,11 +64,6 @@ public function isAutoWriteTimestamp($auto) return $this; } - public function getDateTimeFields(bool $update = false) - { - return $update ? $this->updateTime : [$this->createTime, $this->updateTime]; - } - /** * 检测时间字段的实际类型. *