From 755aa7e2e5a9e292023fd1f686600c57905396f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Thu, 18 Mar 2021 18:15:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Database/SqlBuilder.php | 11 ++++++++++- Database/Traits/QueryTrait.php | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Database/SqlBuilder.php b/Database/SqlBuilder.php index fb7ece63..2f675953 100644 --- a/Database/SqlBuilder.php +++ b/Database/SqlBuilder.php @@ -254,7 +254,16 @@ class SqlBuilder extends Component */ public function tableName(): string { - return $this->query->modelClass::getTable(); + if ($this->query->from instanceof \Closure) { + $this->query->from = call_user_func($this->query->from, new ActiveQuery($this->query->modelClass)); + } + if ($this->query->from instanceof ActiveQuery) { + $this->query->from = '(' . SqlBuilder::builder($this->query->from)->get($this->query->from) . ')'; + } + if (empty($this->query->from)) { + return $this->query->modelClass::getTable(); + } + return $this->query->from; } } diff --git a/Database/Traits/QueryTrait.php b/Database/Traits/QueryTrait.php index 92d6b863..7e1ce8be 100644 --- a/Database/Traits/QueryTrait.php +++ b/Database/Traits/QueryTrait.php @@ -28,7 +28,7 @@ trait QueryTrait public ?int $offset = NULL; public ?int $limit = NULL; public string $group = ''; - public string $from = ''; + public string|\Closure|ActiveQuery $from = ''; public string $alias = 't1'; public array $filter = [];