From afe70d39b9b7f386f0136625c3c55724897027fc Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Sun, 13 Dec 2020 10:16:28 +0800 Subject: [PATCH] =?UTF-8?q?fixed:=E4=BF=AE=E5=A4=8DCatchJoin=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E8=BF=9E=E6=8E=A5=E5=90=8C=E4=B8=80=E4=B8=AA=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E4=B8=A4=E6=AC=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extend/catcher/CatchQuery.php | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/extend/catcher/CatchQuery.php b/extend/catcher/CatchQuery.php index f89c20b..1e2d3a7 100644 --- a/extend/catcher/CatchQuery.php +++ b/extend/catcher/CatchQuery.php @@ -13,7 +13,7 @@ class CatchQuery extends Query /** * * @time 2020年01月13日 - * @param string $model + * @param mixed $model * @param string $joinField * @param string $currentJoinField * @param array $field @@ -21,29 +21,38 @@ class CatchQuery extends Query * @param array $bind * @return CatchQuery */ - public function catchJoin(string $model, string $joinField, string $currentJoinField, array $field = [], string $type = 'INNER', array $bind = []): CatchQuery + public function catchJoin($model, string $joinField, string $currentJoinField, array $field = [], string $type = 'INNER', array $bind = []): CatchQuery { - $table = app($model)->getTable(); + $tableAlias = null; + + if (is_string($model)) { + $table = app($model)->getTable(); + } else { + list($model, $tableAlias) = $model; + $table = app($model)->getTable(); + } // 合并字段 - $this->options['field'] = array_merge($this->options['field'] ?? [], array_map(function ($value) use ($table) { - return $table . '.' . $value; + $this->options['field'] = array_merge($this->options['field'] ?? [], array_map(function ($value) use ($table, $tableAlias) { + return ($tableAlias ? : $table) . '.' . $value; }, $field)); - return $this->join($table, sprintf('%s.%s=%s.%s', $table, $joinField, $this->getAlias(), $currentJoinField), $type, $bind); + return $this->join($tableAlias ? sprintf('%s %s', $table, $tableAlias) : $table + + , sprintf('%s.%s=%s.%s', $tableAlias ? $tableAlias : $table, $joinField, $this->getAlias(), $currentJoinField), $type, $bind); } /** * * @time 2020年01月13日 - * @param string $model + * @param mixed $model * @param string $joinField * @param string $currentJoinField * @param array $field * @param array $bind * @return CatchQuery */ - public function catchLeftJoin(string $model, string $joinField, string $currentJoinField, array $field = [], array $bind = []): CatchQuery + public function catchLeftJoin($model, string $joinField, string $currentJoinField, array $field = [], array $bind = []): CatchQuery { return $this->catchJoin($model, $joinField, $currentJoinField, $field,'LEFT', $bind); } @@ -51,14 +60,14 @@ class CatchQuery extends Query /** * * @time 2020年01月13日 - * @param string $model + * @param mixed $model * @param string $joinField * @param string $currentJoinField * @param array $field * @param array $bind * @return CatchQuery */ - public function catchRightJoin(string $model, string $joinField, string $currentJoinField, array $field = [], array $bind = []): CatchQuery + public function catchRightJoin($model, string $joinField, string $currentJoinField, array $field = [], array $bind = []): CatchQuery { return $this->catchJoin($model, $joinField, $currentJoinField, $field,'RIGHT', $bind); }