Skip to content

Commit

Permalink
修复框架底层bugbug
Browse files Browse the repository at this point in the history
  • Loading branch information
doyouhaobaby committed Sep 15, 2024
1 parent 4b814ed commit 8e639e0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/Leevel/Database/Ddd/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
51 changes: 38 additions & 13 deletions src/Leevel/Database/Ddd/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
};
}
}

0 comments on commit 8e639e0

Please sign in to comment.