From 1a11178875b42363afb9513095a0e12afa96b1d2 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 2 May 2021 04:52:29 +0800 Subject: [PATCH] modify --- Database/ActiveRecord.php | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/Database/ActiveRecord.php b/Database/ActiveRecord.php index b581b8fb..43237297 100644 --- a/Database/ActiveRecord.php +++ b/Database/ActiveRecord.php @@ -342,12 +342,10 @@ class ActiveRecord extends BaseActiveRecord */ public function hasOne(string $modelName, $foreignKey, $localKey): HasOne|ActiveQuery { - if (!isset($this->attributes[$localKey])) { + if (($value = $this->getAttribute($localKey)) === null) { throw new Exception("Need join table primary key."); } - $value = $this->getAttribute($localKey); - $relation = $this->getRelation(); return new HasOne($modelName, $foreignKey, $value, $relation); @@ -363,12 +361,10 @@ class ActiveRecord extends BaseActiveRecord */ public function hasCount($modelName, $foreignKey, $localKey): mixed { - if (!isset($this->attributes[$localKey])) { + if (($value = $this->getAttribute($localKey)) === null) { throw new Exception("Need join table primary key."); } - $value = $this->getAttribute($localKey); - $relation = $this->getRelation(); return new HasCount($modelName, $foreignKey, $value, $relation); @@ -384,12 +380,10 @@ class ActiveRecord extends BaseActiveRecord */ public function hasMany($modelName, $foreignKey, $localKey): mixed { - if (!isset($this->attributes[$localKey])) { + if (($value = $this->getAttribute($localKey)) === null) { throw new Exception("Need join table primary key."); } - $value = $this->getAttribute($localKey); - $relation = $this->getRelation(); return new HasMany($modelName, $foreignKey, $value, $relation); @@ -404,12 +398,10 @@ class ActiveRecord extends BaseActiveRecord */ public function hasIn($modelName, $foreignKey, $localKey): mixed { - if (!isset($this->attributes[$localKey])) { + if (($value = $this->getAttribute($localKey)) === null) { throw new Exception("Need join table primary key."); } - $value = $this->getAttribute($localKey); - $relation = $this->getRelation(); return new HasMany($modelName, $foreignKey, $value, $relation);