Skip to content

Commit

Permalink
实体模型支持自动获取属性及类型定义
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Nov 14, 2024
1 parent 3c519f7 commit 7e9b24a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
48 changes: 40 additions & 8 deletions src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@
abstract class Entity implements JsonSerializable, ArrayAccess, Arrayable, Jsonable
{
private Model $_model;
private bool $_define = true;
private array $_origin = [];
private array $_get = [];
private array $_schema = [];
private array $_data = [];

/**
* 架构函数.
Expand Down Expand Up @@ -91,6 +94,11 @@ protected function initializeData(array | object $data)
$this->_model->setKey($val);
}
}

if ($this->_define) {
// 非属性定义模式下 采用动态属性
$this->_data = $this->_origin;
}
}

/**
Expand Down Expand Up @@ -207,6 +215,10 @@ public function model(): Model
*/
protected function getFields(?string $field = null)
{
if (!empty($this->_schema)) {
return $field ? ($this->_schema[$field] ?? 'string') : $this->_schema;
}

$class = new ReflectionClass($this);
$propertys = $class->getProperties(ReflectionProperty::IS_PUBLIC);
$schema = [];
Expand All @@ -217,6 +229,16 @@ protected function getFields(?string $field = null)
$schema[$name] = $type;
}

if (empty($schema)) {
$this->_define = false;
// 获取数据表信息
$schema = $this->_model->getFieldsType($this->_model->getTable());
$type = $this->_model->getType();
$schema = array_merge($schema, $type);
}

$this->_schema = $schema;

if ($field) {
return $schema[$field] ?? 'string';
}
Expand Down Expand Up @@ -269,7 +291,7 @@ public function save(array | object $data = []): bool
unset($data[$name]);
} elseif ($val instanceof Collection) {
unset($data[$name]);
} elseif (!$isUpdate || $val !== $this->_origin[$name]) {
} elseif (!$isUpdate || (isset($this->_origin[$name]) && $val !== $this->_origin[$name])) {
// 类型转换
$val = $this->writeTransform($val, $this->getFields($name));
$method = 'set' . Str::studly($name) . 'Attr';
Expand Down Expand Up @@ -297,7 +319,6 @@ public function save(array | object $data = []): bool
}

return true;

}

/**
Expand Down Expand Up @@ -497,7 +518,12 @@ function getPublicVars($object)
return get_object_vars($object);
}
};
return $class->getPublicVars($this);

$data = $class->getPublicVars($this);
if (empty($data) && !empty($this->_data)) {
$data = $this->_data;
}
return $data;
}

/**
Expand Down Expand Up @@ -568,7 +594,7 @@ public function get(string $name)
return $this->_get[$name];
}

$value = $this->$name ?? null;
$value = $this->getValue($name);
$method = 'get' . Str::studly($name) . 'Attr';
if (method_exists($this, $method)) {
$value = $this->$method($value, $this->getData());
Expand All @@ -578,6 +604,11 @@ public function get(string $name)
return $value;
}

public function getValue($name)
{
return $this->_define ? ($this->$name ?? null) : ($this->_data[$name] ?? null);
}

/**
* 模型数据转Json.
*
Expand Down Expand Up @@ -612,8 +643,9 @@ public function __get(string $name)
*/
public function __set(string $name, $value): void
{
$name = $this->_model->getRealFieldName($name);
$this->_get[$name] = $value;
$name = $this->_model->getRealFieldName($name);

$this->_data[$name] = $value;
}

/**
Expand All @@ -626,7 +658,7 @@ public function __set(string $name, $value): void
public function __isset(string $name): bool
{
$name = $this->_model->getRealFieldName($name);
return isset($this->$name) || isset($this->_get[$name]);
return isset($this->_data[$name]);
}

/**
Expand All @@ -639,7 +671,7 @@ public function __isset(string $name): bool
public function __unset(string $name): void
{
$name = $this->_model->getRealFieldName($name);
unset($this->_get[$name]);
unset($this->_data[$name]);
}

public function __toString()
Expand Down
5 changes: 5 additions & 0 deletions src/model/concern/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ public function isJsonAssoc(): bool
return $this->jsonAssoc;
}

public function getType()
{
return $this->type;
}

/**
* 判断一个字段名是否为主键字段.
*
Expand Down

0 comments on commit 7e9b24a

Please sign in to comment.