Skip to content

Commit 24dff99

Browse files
authored
Fixed bug that eachById cannot work for Hyperf\Database\Query\Builder. (#7705)
1 parent 1cd7d6e commit 24dff99

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

src/Concerns/BuildsQueries.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public function first($columns = ['*'])
187187
*/
188188
public function eachById(callable $callback, int $count = 1000, ?string $column = null, ?string $alias = null): bool
189189
{
190-
return $this->chunkById($count, function (Collection $results) use ($callback) {
190+
return $this->chunkById($count, function (BaseCollection $results) use ($callback) {
191191
foreach ($results as $value) {
192192
if ($callback($value) === false) {
193193
return false;

tests/ModelRealBuilderTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
use PHPUnit\Framework\TestCase;
6868
use Psr\EventDispatcher\EventDispatcherInterface;
6969
use RuntimeException;
70+
use stdClass;
7071

7172
/**
7273
* @internal
@@ -98,6 +99,25 @@ protected function tearDown(): void
9899
Mockery::close();
99100
}
100101

102+
public function testEachById()
103+
{
104+
$this->getContainer();
105+
106+
$count = User::query()->count();
107+
108+
$i = 0;
109+
User::query()->eachById(function (User $user) use (&$i) {
110+
++$i;
111+
});
112+
$this->assertSame($i, $count);
113+
114+
$i = 0;
115+
Db::table('user')->eachById(function (stdClass $user) use (&$i) {
116+
++$i;
117+
}, 100, 'id');
118+
$this->assertSame($i, $count);
119+
}
120+
101121
public function testPivot()
102122
{
103123
$this->getContainer();

tests/Stubs/ContainerStub.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Hyperf\Database\ConnectionResolverInterface;
2020
use Hyperf\Database\Connectors\ConnectionFactory;
2121
use Hyperf\Database\Connectors\MySqlConnector;
22+
use Hyperf\DbConnection\Db;
2223
use Hyperf\Di\Container;
2324
use Mockery;
2425
use Psr\EventDispatcher\EventDispatcherInterface;
@@ -35,6 +36,7 @@ public static function getContainer($callback = null)
3536
$container->shouldReceive('has')->with(StdoutLoggerInterface::class)->andReturnFalse();
3637
$container->shouldReceive('has')->with(EventDispatcherInterface::class)->andReturnFalse();
3738
$container->shouldReceive('get')->with('db.connector.mysql')->andReturn(new MySqlConnector());
39+
$container->shouldReceive('get')->with(Db::class)->andReturn(new Db($container));
3840
$connector = new ConnectionFactory($container);
3941

4042
$dbConfig = [

0 commit comments

Comments
 (0)