From f023804b283f1d81d4bc57ed0c2bd2e0ba9d8ed7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Fri, 26 Mar 2021 15:08:01 +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/Traits/QueryTrait.php | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/Database/Traits/QueryTrait.php b/Database/Traits/QueryTrait.php index c8e90611..e9d0f99b 100644 --- a/Database/Traits/QueryTrait.php +++ b/Database/Traits/QueryTrait.php @@ -86,15 +86,38 @@ trait QueryTrait * @return $this * @throws Exception */ - public function if(string $condition, string|array $condition1, string|array $condition2): static + public function if(string $condition, string $condition1, string|array $condition2): static { - $condition1 = (new Sql())->where($condition1)->getCondition(); - $condition2 = (new Sql())->where($condition2)->getCondition(); - + if (!is_string($condition1)) { + $condition1 = $this->conditionToString($condition1); + } + if (!is_string($condition2)) { + $condition2 = $this->conditionToString($condition2); + } $this->where[] = 'IF(' . $condition . ',' . $condition1 . ',' . $condition2 . ')'; return $this; } + + /** + * @param $condition + * @return string + * @throws NotFindClassException + * @throws ReflectionException + * @throws Exception + */ + private function conditionToString($condition): string + { + $newSql = $this->makeNewSqlGenerate(); + if ($condition instanceof \Closure) { + call_user_func($condition, $newSql); + } else { + $newSql->where($condition); + } + return $newSql->getCondition(); + } + + /** * @param $bool * @return $this