Skip to content

Commit

Permalink
增加实体模型配置定义方法getOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Nov 19, 2024
1 parent cfb7b32 commit 8157ebe
Showing 1 changed file with 18 additions and 25 deletions.
43 changes: 18 additions & 25 deletions src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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,
];
Expand All @@ -74,6 +77,16 @@ public function __construct(array | object $data = [], ?Model $model = null)
$this->initializeData($data);
}

/**
* 在实体模型中定义 返回相关配置参数.
*
* @return array
*/
protected function getOptions(): array
{
return [];
}

/**
* 解析模型实例名称.
*
Expand Down Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit 8157ebe

Please sign in to comment.