From 17f4f8c8962edce53eb164e38ba227e93ce3d00d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Fri, 26 Mar 2021 14:26:38 +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 | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Database/Traits/QueryTrait.php b/Database/Traits/QueryTrait.php index 7e1ce8be..9aaefbc5 100644 --- a/Database/Traits/QueryTrait.php +++ b/Database/Traits/QueryTrait.php @@ -13,6 +13,7 @@ namespace Database\Traits; use Database\ActiveQuery; use Database\ActiveRecord; use Database\Condition\MathematicsCondition; +use Database\Sql; use Exception; /** @@ -56,6 +57,23 @@ trait QueryTrait $this->filter = []; } + + /** + * @param $condition + * @param $condition1 + * @param $condition2 + * @return $this + * @throws Exception + */ + public function if(string $condition, string|array $condition1, string|array $condition2): static + { + $condition1 = (new Sql())->where($condition1)->getCondition(); + $condition2 = (new Sql())->where($condition2)->getCondition(); + + $this->where[] = 'IF(' . $condition . ',' . $condition1 . ',' . $condition2 . ')'; + return $this; + } + /** * @param $bool * @return $this