Skip to content

Commit

Permalink
修正
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Dec 3, 2024
1 parent 41dde06 commit d4d857d
Show file tree
Hide file tree
Showing 10 changed files with 14 additions and 28 deletions.
6 changes: 2 additions & 4 deletions src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,8 @@ public function __construct(array | object $data = [], ?Model $model = null)

// 设置模型
$this->initModel($model);
if (!empty($data)) {
// 初始化模型数据
$this->initializeData($data);
}
// 初始化模型数据
$this->initializeData($data);
}

public static function instance($data)
Expand Down
18 changes: 3 additions & 15 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,19 +327,6 @@ public static function getEntityClass()
return class_exists($entity) ? $entity: static::class;
}

/**
* 创建新的模型实例.
*
* @param array $data 数据
*
* @return Modelable
*/
public static function instance(array $data =[]): Modelable
{
$class = static::getEntityClass();
return new $class($data, $this);
}

/**
* 创建新的模型实例.
*
Expand All @@ -351,7 +338,8 @@ public static function instance(array $data =[]): Modelable
*/
public function newInstance(array $data = [], $where = null, array $options = []): Modelable
{
$model = static::instance($data);
$class = static::getEntityClass();
$model = new $class($data, $this);

if ($this->connection) {
$model->setConnection($this->connection);
Expand Down Expand Up @@ -590,7 +578,7 @@ public function refresh(bool $relation = false)

if ($relation) {
$this->relation = [];
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/model/relation/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(Model $parent, string $model, string $foreignKey, st
$this->model = $model;
$this->foreignKey = $foreignKey;
$this->localKey = $localKey;
$this->query = $model::instance()->db();
$this->query = (new $model())->newInstance()->db();
$this->relation = $relation;

if (get_class($parent) == $model) {
Expand Down
2 changes: 1 addition & 1 deletion src/model/relation/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function __construct(Model $parent, string $model, string $middle, string
$this->middle = $middle;
}

$this->query = $model::instance()->db();
$this->query = (new $model())->newInstance()->db();
$this->pivot = $this->newPivot();
}

Expand Down
2 changes: 1 addition & 1 deletion src/model/relation/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(Model $parent, string $model, string $foreignKey, st
$this->model = $model;
$this->foreignKey = $foreignKey;
$this->localKey = $localKey;
$this->query = $model::instance()->db();
$this->query = (new $model())->newInstance()->db();

if (get_class($parent) == $model) {
$this->selfRelation = true;
Expand Down
4 changes: 2 additions & 2 deletions src/model/relation/HasManyThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ public function __construct(Model $parent, string $model, string $through, strin
{
$this->parent = $parent;
$this->model = $model;
$this->through = $through::instance()->db();
$this->through = (new $through())->newInstance()->db();
$this->foreignKey = $foreignKey;
$this->throughKey = $throughKey;
$this->localKey = $localKey;
$this->throughPk = $throughPk;
$this->query = $model::instance()->db();
$this->query = (new $model())->newInstance()->db();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/model/relation/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct(Model $parent, string $model, string $foreignKey, st
$this->model = $model;
$this->foreignKey = $foreignKey;
$this->localKey = $localKey;
$this->query = $model::instance()->db();
$this->query = (new $model())->newInstance()->db();

if (get_class($parent) == $model) {
$this->selfRelation = true;
Expand Down
2 changes: 1 addition & 1 deletion src/model/relation/MorphMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function __construct(Model $parent, string $model, string $morphKey, stri
$this->type = $type;
$this->morphKey = $morphKey;
$this->morphType = $morphType;
$this->query = $model::instance()->db();
$this->query = (new $model())->newInstance()->db();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/model/relation/MorphOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function __construct(Model $parent, string $model, string $morphKey, stri
$this->type = $type;
$this->morphKey = $morphKey;
$this->morphType = $morphType;
$this->query = $model::instance()->db();
$this->query = (new $model())->newInstance()->db();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/model/relation/MorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ protected function eagerlyMorphToOne(string $model, string $relation, Model $res
$data = null;

if (class_exists($model)) {
$data = $model::instance()->with($subRelation)
$data = (new $model())->newInstance()->with($subRelation)
->cache($cache[0] ?? false, $cache[1] ?? null, $cache[2] ?? null)
->find($pk);

Expand Down

0 comments on commit d4d857d

Please sign in to comment.