From 8157ebe9dd9d514880b627d45b27bebaa05fc586 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 19 Nov 2024 14:50:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AE=9E=E4=BD=93=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E9=85=8D=E7=BD=AE=E5=AE=9A=E4=B9=89=E6=96=B9=E6=B3=95?= =?UTF-8?q?getOptions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Entity.php | 43 ++++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/src/Entity.php b/src/Entity.php index 919cac8c..95e2844b 100644 --- a/src/Entity.php +++ b/src/Entity.php @@ -51,6 +51,9 @@ public function __construct(array | object $data = [], ?Model $model = null) $model = new $class; } + // 获取实体模型参数 + $options = $this->getOptions(); + if (!self::$weakMap) { self::$weakMap = new WeakMap; } @@ -61,10 +64,10 @@ public function __construct(array | object $data = [], ?Model $model = null) 'origin' => [], 'schema' => [], 'together' => [], - 'hidden' => [], - 'visible' => [], - 'append' => [], - 'mapping' => [], + 'hidden' => $options['hidden'] ?? [], + 'visible' => $options['visible'] ?? [], + 'append' => $options['append'] ?? [], + 'mapping' => $options['mapping'] ?? [], 'strict' => true, 'model' => $model, ]; @@ -74,6 +77,16 @@ public function __construct(array | object $data = [], ?Model $model = null) $this->initializeData($data); } + /** + * 在实体模型中定义 返回相关配置参数. + * + * @return array + */ + protected function getOptions(): array + { + return []; + } + /** * 解析模型实例名称. * @@ -608,32 +621,12 @@ public static function create(array | object $data): Entity */ public static function destroy($data, bool $force = false): bool { - if (empty($data) && 0 !== $data) { - return false; - } - $entity = new static(); if ($entity->isVirtual()) { return true; } - $query = $entity->model()->db(); - - if (is_array($data) && key($data) !== 0) { - $query->where($data); - $data = []; - } elseif ($data instanceof \Closure) { - $data($query); - $data = []; - } - - $resultSet = $query->select((array) $data); - - foreach ($resultSet as $result) { - $result->force($force)->delete(); - } - - return true; + return $entity->model()->destroy($data, $force); } /**