From 6493bf91bf39708028fd384432155aaf2b65a1cd Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sat, 1 Apr 2023 20:48:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Model.php | 61 +++++++++++++++++++++++-------------------------------- 1 file changed, 25 insertions(+), 36 deletions(-) diff --git a/Model.php b/Model.php index 0e697b5..b127231 100644 --- a/Model.php +++ b/Model.php @@ -284,6 +284,28 @@ class Model extends Base\Model } return $relates; } + + + /** + * @param ModelInterface|string $modelName + * @param $foreignKey + * @param $localKey + * @return string + * @throws Exception + */ + private function _hasBase(ModelInterface|string $modelName, $foreignKey, $localKey):string { + if (($value = $this->{$localKey}) === null) { + throw new Exception("Need join table primary key."); + } + + $relation = $this->getRelation(); + + $primaryKey = $modelName . $foreignKey . $value; + if (!$relation->hasIdentification($primaryKey)) { + $relation->bindIdentification($primaryKey, $modelName::query()->where([$foreignKey => $value])); + } + return $primaryKey; + } /** @@ -295,18 +317,7 @@ class Model extends Base\Model */ public function hasOne(ModelInterface|string $modelName, $foreignKey, $localKey): HasOne|ActiveQuery { - if (($value = $this->{$localKey}) === null) { - throw new Exception("Need join table primary key."); - } - - $relation = $this->getRelation(); - - $primaryKey = $modelName . $foreignKey . $value; - if (!$relation->hasIdentification($primaryKey)) { - $relation->bindIdentification($primaryKey, $modelName::query()->where([$foreignKey => $value])); - } - - return new HasOne($primaryKey, $relation); + return new HasOne($this->_hasBase($modelName, $foreignKey, $localKey)); } @@ -319,18 +330,7 @@ class Model extends Base\Model */ public function hasCount(ModelInterface|string $modelName, $foreignKey, $localKey): ActiveQuery|HasCount { - if (($value = $this->{$localKey}) === null) { - throw new Exception("Need join table primary key."); - } - - $relation = $this->getRelation(); - - $primaryKey = $modelName . $foreignKey . $value; - if (!$relation->hasIdentification($primaryKey)) { - $relation->bindIdentification($primaryKey, $modelName::query()->where([$foreignKey => $value])); - } - - return new HasCount($primaryKey); + return new HasCount($this->_hasBase($modelName, $foreignKey, $localKey)); } @@ -343,18 +343,7 @@ class Model extends Base\Model */ public function hasMany(ModelInterface|string $modelName, $foreignKey, $localKey): ActiveQuery|HasMany { - if (($value = $this->{$localKey}) === null) { - throw new Exception("Need join table primary key."); - } - - $relation = $this->getRelation(); - - $primaryKey = $modelName . $foreignKey . $value; - if (!$relation->hasIdentification($primaryKey)) { - $relation->bindIdentification($primaryKey, $modelName::query()->where([$foreignKey => $value])); - } - - return new HasMany($primaryKey); + return new HasMany($this->_hasBase($modelName, $foreignKey, $localKey)); } /**