From 6bab32b1ead4a8a9d3e82fda221cfe5322d2b535 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 7 Sep 2017 11:26:46 +0800 Subject: [PATCH] =?UTF-8?q?hasOne=E5=85=B3=E8=81=94=E7=9A=84has=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E6=94=B9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/relation/HasOne.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/library/think/model/relation/HasOne.php b/library/think/model/relation/HasOne.php index bb95ef19..bb9f3286 100644 --- a/library/think/model/relation/HasOne.php +++ b/library/think/model/relation/HasOne.php @@ -67,11 +67,14 @@ class HasOne extends OneToOne public function has() { $table = $this->query->getTable(); + $model = basename(str_replace('\\', '/', get_class($this->parent))); + $relation = basename(str_replace('\\', '/', $this->model)); $localKey = $this->localKey; $foreignKey = $this->foreignKey; - return $this->parent->db()->alias('a') - ->whereExists(function ($query) use ($table, $localKey, $foreignKey) { - $query->table([$table => 'b'])->field('b.' . $foreignKey)->whereExp('a.' . $localKey, '=b.' . $foreignKey); + return $this->parent->db() + ->alias($model) + ->whereExists(function ($query) use ($table, $model, $relation, $localKey, $foreignKey) { + $query->table([$table => $relation])->field($relation . '.' . $foreignKey)->whereExp($model . '.' . $localKey, '=' . $relatoin . '.' . $foreignKey); }); }