Skip to content

Commit

Permalink
改进模型create和update方法支持实体模型返回
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Nov 18, 2024
1 parent 0a6dbfe commit 00aed1c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
40 changes: 21 additions & 19 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,6 @@ public function __construct(array | object $data = [])
}
}

$this->parseEntity();
// 执行初始化操作
$this->initialize();
}
Expand Down Expand Up @@ -318,16 +317,14 @@ public function setName(string $name)
}

/**
* 解析模型实例名称.
* 获取模型类名.
*
* @return void
* @return string
*/
protected function parseEntity()
public static function getEntityClass()
{
if (!$this->entityClass) {
$entity = str_replace('\\model', '\\entity', static::class);
$this->entityClass = class_exists($entity) ? $entity: static::class;
}
$entity = str_replace('\\model', '\\entity', static::class);
return class_exists($entity) ? $entity: static::class;
}

/**
Expand All @@ -339,9 +336,10 @@ protected function parseEntity()
*
* @return Model
*/
public function newInstance(array $data = [], $where = null, array $options = [])
public function newInstance(array $data = [], $where = null, array $options = []): Modelable
{
$model = new $this->entityClass($data, $this);
$class = static::getEntityClass();
$model = new $class($data, $this);

if ($this->connection) {
$model->setConnection($this->connection);
Expand Down Expand Up @@ -839,11 +837,11 @@ protected function insertData(array $data, ?string $sequence = null): bool
if ($this->isAutoWriteId()) {
$pk = $this->getPk();
if (is_string($pk) && !isset($this->data[$pk])) {
$data[$pk] = $this->autoWriteId();
$data[$pk] = $this->autoWriteId();
if ($this->entity) {
$this->entity->$pk = $data[$pk];
} else {
$this->data[$pk] = $data[$pk];
$this->data[$pk] = $data[$pk];
}
}
}
Expand All @@ -868,7 +866,7 @@ protected function insertData(array $data, ?string $sequence = null): bool
$field = is_string($name) ? $name : $val;
if (!isset($data[$field])) {
if (is_string($name)) {
$data[$field] = $val;
$data[$field] = $val;
$this->data[$field] = $val;
} else {
$this->setAttr($field, null);
Expand Down Expand Up @@ -1038,9 +1036,10 @@ public function delete(): bool
*
* @return static
*/
public static function create(array | object $data, array $allowField = [], bool $replace = false, string $suffix = ''): Model
public static function create(array | object $data, array $allowField = [], bool $replace = false, string $suffix = ''): Modelable
{
$model = new static();
$class = static::getEntityClass();
$model = new $class();

if (!empty($allowField)) {
$model->allowField($allowField);
Expand All @@ -1050,7 +1049,8 @@ public static function create(array | object $data, array $allowField = [], bool
$model->setSuffix($suffix);
}

$model->replace($replace)->save($data);
$model->replace($replace);
$model->save($data);

return $model;
}
Expand All @@ -1065,9 +1065,10 @@ public static function create(array | object $data, array $allowField = [], bool
*
* @return static
*/
public static function update(array | object $data, $where = [], array $allowField = [], string $suffix = '')
public static function update(array | object $data, $where = [], array $allowField = [], string $suffix = ''): Modelable
{
$model = new static();
$class = static::getEntityClass();
$model = new $class();

if (!empty($allowField)) {
$model->allowField($allowField);
Expand All @@ -1081,7 +1082,8 @@ public static function update(array | object $data, $where = [], array $allowFie
$model->setSuffix($suffix);
}

$model->exists(true)->save($data);
$model->exists(true);
$model->save($data);

return $model;
}
Expand Down
4 changes: 4 additions & 0 deletions src/model/concern/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ public function data(array | object $data, bool $set = false, array $allow = [])
$data = get_object_vars($data);
}

if (empty($data)) {
return $this;
}

// 清空数据
$this->data = [];

Expand Down

0 comments on commit 00aed1c

Please sign in to comment.