Skip to content

Commit f95dfb4

Browse files
committed
Merge branch '5.7'
2 parents 59838f8 + 6e0c87d commit f95dfb4

File tree

6 files changed

+218
-1
lines changed

6 files changed

+218
-1
lines changed

.editorconfig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,4 @@ trim_trailing_whitespace = true
1212
trim_trailing_whitespace = false
1313

1414
[*.yml]
15-
indent_style = space
1615
indent_size = 2

src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,32 @@ public function chunk($count, callable $callback)
678678
});
679679
}
680680

681+
/**
682+
* Chunk the results of a query by comparing numeric IDs.
683+
*
684+
* @param int $count
685+
* @param callable $callback
686+
* @param string|null $column
687+
* @param string|null $alias
688+
* @return bool
689+
*/
690+
public function chunkById($count, callable $callback, $column = null, $alias = null)
691+
{
692+
$this->query->addSelect($this->shouldSelect());
693+
694+
$column = $column ?? $this->getRelated()->qualifyColumn(
695+
$this->getRelatedKeyName()
696+
);
697+
698+
$alias = $alias ?? $this->getRelatedKeyName();
699+
700+
return $this->query->chunkById($count, function ($results) use ($callback) {
701+
$this->hydratePivotRelation($results->all());
702+
703+
return $callback($results);
704+
}, $column, $alias);
705+
}
706+
681707
/**
682708
* Execute a callback over each item while chunking.
683709
*

src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,24 @@ public function chunk($count, callable $callback)
427427
return $this->prepareQueryBuilder()->chunk($count, $callback);
428428
}
429429

430+
/**
431+
* Chunk the results of a query by comparing numeric IDs.
432+
*
433+
* @param int $count
434+
* @param callable $callback
435+
* @param string|null $column
436+
* @param string|null $alias
437+
* @return bool
438+
*/
439+
public function chunkById($count, callable $callback, $column = null, $alias = null)
440+
{
441+
$column = $column ?? $this->getRelated()->getQualifiedKeyName();
442+
443+
$alias = $alias ?? $this->getRelated()->getKeyName();
444+
445+
return $this->prepareQueryBuilder()->chunkById($count, $callback, $column, $alias);
446+
}
447+
430448
/**
431449
* Get a generator for the given query.
432450
*
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Database;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Illuminate\Database\Connection;
7+
use Illuminate\Database\Eloquent\Collection;
8+
use Illuminate\Database\Capsule\Manager as DB;
9+
use Illuminate\Database\Eloquent\Model as Eloquent;
10+
11+
class DatabaseEloquentBelongsToManyChunkByIdTest extends TestCase
12+
{
13+
public function setUp()
14+
{
15+
$db = new DB;
16+
17+
$db->addConnection([
18+
'driver' => 'sqlite',
19+
'database' => ':memory:',
20+
]);
21+
22+
$db->bootEloquent();
23+
$db->setAsGlobal();
24+
25+
$this->createSchema();
26+
}
27+
28+
/**
29+
* Setup the database schema.
30+
*
31+
* @return void
32+
*/
33+
public function createSchema()
34+
{
35+
$this->schema()->create('users', function ($table) {
36+
$table->increments('id');
37+
$table->string('email')->unique();
38+
});
39+
40+
$this->schema()->create('articles', function ($table) {
41+
$table->increments('aid');
42+
$table->string('title');
43+
});
44+
45+
$this->schema()->create('article_user', function ($table) {
46+
$table->integer('article_id')->unsigned();
47+
$table->foreign('article_id')->references('aid')->on('articles');
48+
$table->integer('user_id')->unsigned();
49+
$table->foreign('user_id')->references('id')->on('users');
50+
});
51+
}
52+
53+
public function testBelongsToChunkById()
54+
{
55+
$this->seedData();
56+
57+
$user = BelongsToManyChunkByIdTestTestUser::query()->first();
58+
$i = 0;
59+
60+
$user->articles()->chunkById(1, function (Collection $collection) use (&$i) {
61+
$i++;
62+
$this->assertTrue($collection->first()->aid == $i);
63+
});
64+
65+
$this->assertTrue($i === 3);
66+
}
67+
68+
/**
69+
* Tear down the database schema.
70+
*
71+
* @return void
72+
*/
73+
public function tearDown()
74+
{
75+
$this->schema()->drop('users');
76+
$this->schema()->drop('articles');
77+
$this->schema()->drop('article_user');
78+
}
79+
80+
/**
81+
* Helpers...
82+
*/
83+
protected function seedData()
84+
{
85+
$user = BelongsToManyChunkByIdTestTestUser::create(['id' => 1, 'email' => '[email protected]']);
86+
BelongsToManyChunkByIdTestTestArticle::query()->insert([
87+
['aid' => 1, 'title' => 'Another title'],
88+
['aid' => 2, 'title' => 'Another title'],
89+
['aid' => 3, 'title' => 'Another title'],
90+
]);
91+
92+
$user->articles()->sync([3, 1, 2]);
93+
}
94+
95+
/**
96+
* Get a database connection instance.
97+
*
98+
* @return Connection
99+
*/
100+
protected function connection()
101+
{
102+
return Eloquent::getConnectionResolver()->connection();
103+
}
104+
105+
/**
106+
* Get a schema builder instance.
107+
*
108+
* @return Schema\Builder
109+
*/
110+
protected function schema()
111+
{
112+
return $this->connection()->getSchemaBuilder();
113+
}
114+
}
115+
116+
class BelongsToManyChunkByIdTestTestUser extends Eloquent
117+
{
118+
protected $table = 'users';
119+
protected $fillable = ['id', 'email'];
120+
public $timestamps = false;
121+
122+
public function articles()
123+
{
124+
return $this->belongsToMany(BelongsToManyChunkByIdTestTestArticle::class, 'article_user', 'user_id', 'article_id');
125+
}
126+
}
127+
128+
class BelongsToManyChunkByIdTestTestArticle extends Eloquent
129+
{
130+
protected $primaryKey = 'aid';
131+
protected $table = 'articles';
132+
protected $keyType = 'string';
133+
public $incrementing = false;
134+
public $timestamps = false;
135+
protected $fillable = ['aid', 'title'];
136+
}

tests/Database/DatabaseEloquentHasManyThroughIntegrationTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,24 @@ public function testChunkReturnsCorrectModels()
199199
});
200200
}
201201

202+
public function testChunkById()
203+
{
204+
$this->seedData();
205+
$this->seedDataExtended();
206+
$country = HasManyThroughTestCountry::find(2);
207+
208+
$i = 0;
209+
$count = 0;
210+
211+
$country->posts()->chunkById(2, function ($collection) use (&$i, &$count) {
212+
$i++;
213+
$count += $collection->count();
214+
});
215+
216+
$this->assertEquals(3, $i);
217+
$this->assertEquals(6, $count);
218+
}
219+
202220
public function testCursorReturnsCorrectModels()
203221
{
204222
$this->seedData();

tests/Database/DatabaseEloquentPolymorphicRelationsIntegrationTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,26 @@ public function testEagerLoading()
104104
$this->assertEquals($post->id, $tag->posts->first()->id);
105105
}
106106

107+
public function testChunkById()
108+
{
109+
$post = EloquentManyToManyPolymorphicTestPost::create();
110+
$tag1 = EloquentManyToManyPolymorphicTestTag::create();
111+
$tag2 = EloquentManyToManyPolymorphicTestTag::create();
112+
$tag3 = EloquentManyToManyPolymorphicTestTag::create();
113+
$post->tags()->attach([$tag1->id, $tag2->id, $tag3->id]);
114+
115+
$count = 0;
116+
$iterations = 0;
117+
$post->tags()->chunkById(2, function ($tags) use (&$iterations, &$count) {
118+
$this->assertInstanceOf(EloquentManyToManyPolymorphicTestTag::class, $tags->first());
119+
$count += $tags->count();
120+
$iterations++;
121+
});
122+
123+
$this->assertEquals(2, $iterations);
124+
$this->assertEquals(3, $count);
125+
}
126+
107127
/**
108128
* Helpers...
109129
*/

0 commit comments

Comments
 (0)