Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.7] Added chunkById support in relations. #26919

Merged
merged 4 commits into from
Dec 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,29 @@ public function chunk($count, callable $callback)
});
}

/**
* Chunk the results of a query by comparing numeric IDs.
*
* @param int $count
* @param callable $callback
* @param string|null $column
* @param string|null $alias
* @return bool
*/
public function chunkById($count, callable $callback, $column = null, $alias = null)
{
$this->query->addSelect($this->shouldSelect());

$column = $column ?? $this->getRelated()->qualifyColumn($this->getRelatedKeyName());
$alias = $alias ?? $this->getRelatedKeyName();

return $this->query->chunkById($count, function ($results) use ($callback) {
$this->hydratePivotRelation($results->all());

return $callback($results);
}, $column, $alias);
}

/**
* Execute a callback over each item while chunking.
*
Expand Down
17 changes: 17 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,23 @@ public function chunk($count, callable $callback)
return $this->prepareQueryBuilder()->chunk($count, $callback);
}

/**
* Chunk the results of a query by comparing numeric IDs.
*
* @param int $count
* @param callable $callback
* @param string|null $column
* @param string|null $alias
* @return bool
*/
public function chunkById($count, callable $callback, $column = null, $alias = null)
{
$column = $column ?? $this->getRelated()->getQualifiedKeyName();
$alias = $alias ?? $this->getRelated()->getKeyName();

return $this->prepareQueryBuilder()->chunkById($count, $callback, $column, $alias);
}

/**
* Get a generator for the given query.
*
Expand Down
136 changes: 136 additions & 0 deletions tests/Database/DatabaseEloquentBelongsToManyChunkByIdTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

namespace Illuminate\Tests\Database;

use PHPUnit\Framework\TestCase;
use Illuminate\Database\Connection;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Model as Eloquent;

class DatabaseEloquentBelongsToManyChunkByIdTest extends TestCase
{
public function setUp()
{
$db = new DB;

$db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
]);

$db->bootEloquent();
$db->setAsGlobal();

$this->createSchema();
}

/**
* Setup the database schema.
*
* @return void
*/
public function createSchema()
{
$this->schema()->create('users', function ($table) {
$table->increments('id');
$table->string('email')->unique();
});

$this->schema()->create('articles', function ($table) {
$table->increments('aid');
$table->string('title');
});

$this->schema()->create('article_user', function ($table) {
$table->integer('article_id')->unsigned();
$table->foreign('article_id')->references('aid')->on('articles');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
});
}

public function testBelongsToChunkById()
{
$this->seedData();

$user = BelongsToManyChunkByIdTestTestUser::query()->first();
$i = 0;

$user->articles()->chunkById(1, function (Collection $collection) use (&$i) {
$i++;
$this->assertTrue($collection->first()->aid == $i);
});

$this->assertTrue($i === 3);
}

/**
* Tear down the database schema.
*
* @return void
*/
public function tearDown()
{
$this->schema()->drop('users');
$this->schema()->drop('articles');
$this->schema()->drop('article_user');
}

/**
* Helpers...
*/
protected function seedData()
{
$user = BelongsToManyChunkByIdTestTestUser::create(['id' => 1, 'email' => '[email protected]']);
BelongsToManyChunkByIdTestTestArticle::query()->insert([
['aid' => 1, 'title' => 'Another title'],
['aid' => 2, 'title' => 'Another title'],
['aid' => 3, 'title' => 'Another title'],
]);

$user->articles()->sync([3, 1, 2]);
}

/**
* Get a database connection instance.
*
* @return Connection
*/
protected function connection()
{
return Eloquent::getConnectionResolver()->connection();
}

/**
* Get a schema builder instance.
*
* @return Schema\Builder
*/
protected function schema()
{
return $this->connection()->getSchemaBuilder();
}
}

class BelongsToManyChunkByIdTestTestUser extends Eloquent
{
protected $table = 'users';
protected $fillable = ['id', 'email'];
public $timestamps = false;

public function articles()
{
return $this->belongsToMany(BelongsToManyChunkByIdTestTestArticle::class, 'article_user', 'user_id', 'article_id');
}
}

class BelongsToManyChunkByIdTestTestArticle extends Eloquent
{
protected $primaryKey = 'aid';
protected $table = 'articles';
protected $keyType = 'string';
public $incrementing = false;
public $timestamps = false;
protected $fillable = ['aid', 'title'];
}
18 changes: 18 additions & 0 deletions tests/Database/DatabaseEloquentHasManyThroughIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,24 @@ public function testChunkReturnsCorrectModels()
});
}

public function testChunkById()
{
$this->seedData();
$this->seedDataExtended();
$country = HasManyThroughTestCountry::find(2);

$i = 0;
$count = 0;

$country->posts()->chunkById(2, function ($collection) use (&$i, &$count) {
$i++;
$count += $collection->count();
});

$this->assertEquals(3, $i);
$this->assertEquals(6, $count);
}

public function testCursorReturnsCorrectModels()
{
$this->seedData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,26 @@ public function testEagerLoading()
$this->assertEquals($post->id, $tag->posts->first()->id);
}

public function testChunkById()
{
$post = EloquentManyToManyPolymorphicTestPost::create();
$tag1 = EloquentManyToManyPolymorphicTestTag::create();
$tag2 = EloquentManyToManyPolymorphicTestTag::create();
$tag3 = EloquentManyToManyPolymorphicTestTag::create();
$post->tags()->attach([$tag1->id, $tag2->id, $tag3->id]);

$count = 0;
$iterations = 0;
$post->tags()->chunkById(2, function ($tags) use (&$iterations, &$count) {
$this->assertInstanceOf(EloquentManyToManyPolymorphicTestTag::class, $tags->first());
$count += $tags->count();
$iterations++;
});

$this->assertEquals(2, $iterations);
$this->assertEquals(3, $count);
}

/**
* Helpers...
*/
Expand Down