From 8e639e02685b9565d4e28cb32bb1d4195624e349 Mon Sep 17 00:00:00 2001 From: Xiangmin Liu <635750556@qq.com> Date: Sun, 15 Sep 2024 13:45:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=A1=86=E6=9E=B6=E5=BA=95?= =?UTF-8?q?=E5=B1=82bugbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Leevel/Database/Ddd/Entity.php | 2 +- src/Leevel/Database/Ddd/Repository.php | 51 +++++++++++++++++++------- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/Leevel/Database/Ddd/Entity.php b/src/Leevel/Database/Ddd/Entity.php index 5e524f5e2..d88226e42 100644 --- a/src/Leevel/Database/Ddd/Entity.php +++ b/src/Leevel/Database/Ddd/Entity.php @@ -729,7 +729,7 @@ public static function select(int $softDeletedType = self::WITHOUT_SOFT_DELETED) if ($withoutGlobalScopeNames && \in_array($scopeName, $withoutGlobalScopeNames, true)) { continue; } - $call($batabaseSelect, $entity); + $call($batabaseSelect, $select, $entity); } } diff --git a/src/Leevel/Database/Ddd/Repository.php b/src/Leevel/Database/Ddd/Repository.php index 647091bfc..5b440e33d 100644 --- a/src/Leevel/Database/Ddd/Repository.php +++ b/src/Leevel/Database/Ddd/Repository.php @@ -12,6 +12,7 @@ use Leevel\Database\ISelect as IDatabaseSelect; use Leevel\Database\Page; use Leevel\Database\Select as DatabaseSelect; +use Leevel\Database\Ddd\Select; use Leevel\Di\Container; use Leevel\Di\IContainer; use Leevel\Event\IDispatch; @@ -792,24 +793,24 @@ public function findCollection(): Collection|EntityCollection public function findAll(?\Closure $condition = null): array|Collection|EntityCollection { // @phpstan-ignore-next-line - return $this->callSelectMethod(__FUNCTION__, $condition); + return $this->callSelectMethodCondition(__FUNCTION__, $condition); } public function find(?int $num = null, ?\Closure $condition = null): array|Collection|EntityCollection { // @phpstan-ignore-next-line - return $this->callSelectMethod(__FUNCTION__, $num, $condition); + return $this->callSelectMethodCondition(__FUNCTION__, $num, $condition); } public function value(string $field, ?\Closure $condition = null): mixed { - return $this->callSelectMethod(__FUNCTION__, $field, $condition); + return $this->callSelectMethodCondition(__FUNCTION__, $field, $condition); } public function findList(mixed $fieldValue, ?string $fieldKey = null, ?\Closure $condition = null): array { // @phpstan-ignore-next-line - return $this->callSelectMethod(__FUNCTION__, $fieldValue, $fieldKey, $condition); + return $this->callSelectMethodCondition(__FUNCTION__, $fieldValue, $fieldKey, $condition); } public function chunk(int $count, \Closure $chunk): void @@ -825,45 +826,45 @@ public function each(int $count, \Closure $each): void public function findCount(string $field = '*', ?\Closure $condition = null): int { // @phpstan-ignore-next-line - return $this->callSelectMethod(__FUNCTION__, $field, $condition); + return $this->callSelectMethodCondition(__FUNCTION__, $field, $condition); } public function findAvg(string $field, ?\Closure $condition = null): mixed { - return $this->callSelectMethod(__FUNCTION__, $field, $condition); + return $this->callSelectMethodCondition(__FUNCTION__, $field, $condition); } public function findMax(string $field, ?\Closure $condition = null): mixed { - return $this->callSelectMethod(__FUNCTION__, $field, $condition); + return $this->callSelectMethodCondition(__FUNCTION__, $field, $condition); } public function findMin(string $field, ?\Closure $condition = null): mixed { - return $this->callSelectMethod(__FUNCTION__, $field, $condition); + return $this->callSelectMethodCondition(__FUNCTION__, $field, $condition); } public function findSum(string $field, ?\Closure $condition = null): mixed { - return $this->callSelectMethod(__FUNCTION__, $field, $condition); + return $this->callSelectMethodCondition(__FUNCTION__, $field, $condition); } public function findPageList(int $currentPage, int $perPage = 10, ?int $count = null, array $config = [], ?\Closure $condition = null): Page { // @phpstan-ignore-next-line - return $this->callSelectMethod(__FUNCTION__, $currentPage, $perPage, $count, $config, $condition); + return $this->callSelectMethodCondition(__FUNCTION__, $currentPage, $perPage, $count, $config, $condition); } public function findPageMacro(int $currentPage, int $perPage = 10, array $config = [], ?\Closure $condition = null): Page { // @phpstan-ignore-next-line - return $this->callSelectMethod(__FUNCTION__, $currentPage, $perPage, $config, $condition); + return $this->callSelectMethodCondition(__FUNCTION__, $currentPage, $perPage, $config, $condition); } public function findPagePrevNext(int $currentPage, int $perPage = 10, array $config = [], ?\Closure $condition = null): Page { // @phpstan-ignore-next-line - return $this->callSelectMethod(__FUNCTION__, $currentPage, $perPage, $config, $condition); + return $this->callSelectMethodCondition(__FUNCTION__, $currentPage, $perPage, $config, $condition); } public function cache(string $name, ?int $expire = null, ?ICache $cache = null): Select @@ -909,7 +910,10 @@ public function insertAll(array $data, array $bind = [], array|bool $replace = f */ public function findPage(int $currentPage, int $perPage = 10, string $column = '*', array $config = [], ?\Closure $condition = null): Page { - if (!$this->getEntitySelect()->getContainer()->enabledCoroutine()) { + if (!$this + ->getEntitySelect() + ->getContainer() + ->enabledCoroutine()) { $count = $this->findCount($column, $condition); $page = $this->findPageList($currentPage, $perPage, null, $config, $condition); $page->setTotalRecord($count); @@ -1060,4 +1064,25 @@ protected function callSelectMethod(string $method, mixed ...$args): mixed { return $this->getEntitySelect()->{$method}(...$args); } + + protected function callSelectMethodCondition(string $method, mixed ...$args): mixed + { + $entitySelect = $this->getEntitySelect(); + $lastIndex = count($args)-1; + // @phpstan-ignore-next-line + $args[$lastIndex] = $this->prepareCondition($entitySelect, $args[$lastIndex]); + + return $this->getEntitySelect()->{$method}(...$args); + } + + protected function prepareCondition(Select $entitySelect, ?\Closure $condition = null): ?\Closure + { + if (!$condition) { + return null; + } + + return function(DatabaseSelect $databaseSelect) use($entitySelect, $condition) :void { + $condition($databaseSelect, $entitySelect, $this->entity); + }; + } }