From 57ada37cf46f38c308e35e6a8903ebd64d7b7e72 Mon Sep 17 00:00:00 2001 From: Arif Hoque Date: Fri, 9 Jan 2026 18:58:50 +0600 Subject: [PATCH 1/2] new entity orm method groupByCallback --- src/Phaseolies/Database/Entity/Builder.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/Phaseolies/Database/Entity/Builder.php b/src/Phaseolies/Database/Entity/Builder.php index bd59359..59f7dc0 100644 --- a/src/Phaseolies/Database/Entity/Builder.php +++ b/src/Phaseolies/Database/Entity/Builder.php @@ -2986,6 +2986,28 @@ public function repair(callable $fixer, bool $saveChanges = false): Collection return $results; } + /** + * Get records grouped by a callback result + * + * @param callable $callback + * @return array + */ + public function groupByCallback(callable $callback): array + { + $results = $this->get(); + $grouped = []; + + foreach ($results as $item) { + $key = $callback($item); + if (!isset($grouped[$key])) { + $grouped[$key] = []; + } + $grouped[$key][] = $item; + } + + return $grouped; + } + /** * Convert camelCase to snake_case for column names * From 514d9e6626e3a5bbdbbb8e174ddcbe7761dc0dc4 Mon Sep 17 00:00:00 2001 From: Arif Hoque Date: Thu, 15 Jan 2026 18:46:59 +0600 Subject: [PATCH 2/2] Unit test for groupByCallback --- tests/Model/Query/EntityModelQueryTest.php | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/Model/Query/EntityModelQueryTest.php b/tests/Model/Query/EntityModelQueryTest.php index d0cde5d..8565cb0 100644 --- a/tests/Model/Query/EntityModelQueryTest.php +++ b/tests/Model/Query/EntityModelQueryTest.php @@ -2,6 +2,7 @@ namespace Tests\Unit\Model\Query; +use App\Models\Product; use Tests\Support\Model\MockUser; use Tests\Support\Model\MockTag; use Tests\Support\Model\MockPost; @@ -2661,4 +2662,33 @@ public function testRepairMethodSaveChanges() $this->assertNull($product->price); } } + + public function testGroupByCallback() + { + $products = MockProduct::groupByCallback(function ($product) { + if ($product->price < 500) return 'lower_than_500'; + if ($product->price > 500) return 'bigger_than_500'; + return 'default'; + }); + + $this->assertCount(1, $products['lower_than_500']); + + foreach ($products['lower_than_500'] as $key => $product) { + $this->assertEquals( + 435, + $product->price, + "Expected product ID {$product->id} to have price 435" + ); + } + + $this->assertCount(2, $products['bigger_than_500']); + + foreach ($products['bigger_than_500'] as $product) { + $this->assertGreaterThan( + 500, + $product->price, + "Expected product ID {$product->id} to have price > 500" + ); + } + } }