Skip to content

Commit

Permalink
调整字段schema
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Nov 21, 2024
1 parent 642e7bf commit c99f00b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 33 deletions.
75 changes: 42 additions & 33 deletions src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,24 @@ public function __construct(array | object $data = [], ?Model $model = null)
}

self::$weakMap[$this] = [
'model' => $model,
'get' => [],
'data' => [],
'origin' => [],
'together' => [],
'allow' => [],
'strict_mode' => true,
'type' => $options['type'] ?? [],
'readonly' => $options['readonly'] ?? [],
'disuse' => $options['disuse'] ?? [],
'hidden' => $options['hidden'] ?? [],
'visible' => $options['visible'] ?? [],
'append' => $options['append'] ?? [],
'mapping' => $options['mapping'] ?? [],
'strict' => $options['strict'] ?? true,
'model' => $model,
'get' => [],
'data' => [],
'schema' => [],
'origin' => [],
'together' => [],
'allow' => [],
'strict_mode' => true,
'type' => $options['type'] ?? [],
'virtual' => $options['virtual'] ?? false,
'readonly' => $options['readonly'] ?? [],
'disuse' => $options['disuse'] ?? [],
'hidden' => $options['hidden'] ?? [],
'visible' => $options['visible'] ?? [],
'append' => $options['append'] ?? [],
'mapping' => $options['mapping'] ?? [],
'strict' => $options['strict'] ?? true,
'relation_keys' => $options['relation_keys'] ?? [],
];

$model->setEntity($this);
Expand Down Expand Up @@ -109,7 +112,7 @@ protected function parseModel(): string
*/
public function model(): Model
{
return self::$weakMap[$this]['model'];
return self::$weakMap[$this]['model']->schema(self::$_schema[static::class][0]);
}

/**
Expand Down Expand Up @@ -321,8 +324,8 @@ protected function getFields(?string $field = null)
// 采用非严格模式
$this->setWeakData('strict_mode', false);
// 获取数据表信息
$fields = $this->model()->getFieldsType($this->model()->getTable());
$array = array_merge($fields, self::$weakMap[$this]['type'] ?: $this->model()->getType());
$fields = self::$weakMap[$this]['model']->getFieldsType(self::$weakMap[$this]['model']->getTable());
$array = array_merge($fields, self::$weakMap[$this]['type'] ?: self::$weakMap[$this]['model']->getType());
foreach ($array as $name => $type) {
$name = $this->getRealFieldName($name);
$schema[$name] = $type;
Expand Down Expand Up @@ -554,7 +557,6 @@ public function save(array | object $data = []): bool
if (!self::$weakMap[$this]['strict']) {
// 非严格模式下 自动转换为小写下划线规范
foreach ($data as $name => $val) {
// 属性名称转换
$trueName = Str::snake($name);
if ($trueName != $name) {
$data[$trueName] = $val;
Expand Down Expand Up @@ -601,8 +603,8 @@ protected function relationSave(array $relations = [])
foreach ($relations as $name => $relation) {
if (in_array($name, $this->getWeakData('together'))) {
$relationKey = $this->getRelationKey($name);
if ($relationKey && property_exists($relation, $relationKey)) {
$relation->$relationKey = self::$weakMap[$this]['model']->getKey();
if ($relationKey) {
$relation->$relationKey = $this->model()->getKey();
}
$relation->save();
}
Expand All @@ -613,7 +615,7 @@ protected function relationSave(array $relations = [])
* 删除模型关联数据(一对一).
*
* @param array $relations 数据
* @return bool
* @return void
*/
protected function relationDelete(array $relations = [])
{
Expand All @@ -624,21 +626,28 @@ protected function relationDelete(array $relations = [])
}
}

protected function getRelationKeys(): array
{
return [];
}

/**
* 获取关联的外键名.
*
* @param string $relation 关联名
* @return string|null
*/
protected function getRelationKey(string $relation)
{
$relationKey = $this->getRelationKeys();
$relationKey = self::$weakMap[$this]['relation_keys'];
return $relationKey[$relation] ?? null;
}

public function isVirtual()
/**
* 是否为虚拟模型(不能写入).
*
* @return bool
*/
public function isVirtual(): bool
{
return false;
return self::$weakMap[$this]['virtual'];
}

/**
* 删除模型数据.
*
Expand Down Expand Up @@ -691,7 +700,7 @@ public static function create(array | object $data, array $allowField = [], bool
}

/**
* 写入数据.
* 更新数据.
*
* @param array|object $data 数据
* @param mixed $where 更新条件
Expand Down Expand Up @@ -775,9 +784,9 @@ function getPublicVars($object)
* 获取原始数据.
*
* @param string|null $name 字段名
* @return array
* @return mixed
*/
public function getOrigin(?string $name = null): array
public function getOrigin(?string $name = null)
{
if ($name) {
return self::$weakMap[$this]['origin'][$name] ?? null;
Expand Down
4 changes: 4 additions & 0 deletions src/db/concern/TableFieldInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public function getTableFields(string $tableName = ''): array
$tableName = $this->getTable();
}

if (!empty($this->options['field_type'])) {
return array_keys($this->options['field_type']);
}

return $this->connection->getTableFields($tableName);
}

Expand Down

0 comments on commit c99f00b

Please sign in to comment.