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

[10.x] Added eachById and chunkByIdDesc to BelongsToMany #50991

Merged
merged 3 commits into from
Apr 10, 2024
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
55 changes: 51 additions & 4 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -997,19 +997,66 @@ public function chunk($count, callable $callback)
*/
public function chunkById($count, callable $callback, $column = null, $alias = null)
{
$this->prepareQueryBuilder();
return $this->orderedChunkById($count, $callback, $column, $alias);
}

/**
* Chunk the results of a query by comparing IDs in descending order.
*
* @param int $count
* @param callable $callback
* @param string|null $column
* @param string|null $alias
* @return bool
*/
public function chunkByIdDesc($count, callable $callback, $column = null, $alias = null)
{
return $this->orderedChunkById($count, $callback, $column, $alias, descending: true);
}

/**
* Execute a callback over each item while chunking by ID.
*
* @param callable $callback
* @param int $count
* @param string|null $column
* @param string|null $alias
* @return bool
*/
public function eachById(callable $callback, $count = 1000, $column = null, $alias = null)
{
return $this->chunkById($count, function ($results, $page) use ($callback, $count) {
foreach ($results as $key => $value) {
if ($callback($value, (($page - 1) * $count) + $key) === false) {
return false;
}
}
}, $column, $alias);
}

/**
* Chunk the results of a query by comparing IDs in a given order.
*
* @param int $count
* @param callable $callback
* @param string|null $column
* @param string|null $alias
* @param bool $descending
* @return bool
*/
public function orderedChunkById($count, callable $callback, $column = null, $alias = null, $descending = false)
{
$column ??= $this->getRelated()->qualifyColumn(
$this->getRelatedKeyName()
);

$alias ??= $this->getRelatedKeyName();

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

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

/**
Expand Down
31 changes: 23 additions & 8 deletions tests/Database/DatabaseEloquentBelongsToManyChunkByIdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ public function createSchema()
});

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

$this->schema()->create('article_user', function ($table) {
$table->increments('id');
$table->integer('article_id')->unsigned();
$table->foreign('article_id')->references('aid')->on('articles');
$table->foreign('article_id')->references('id')->on('articles');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
});
Expand All @@ -58,7 +59,22 @@ public function testBelongsToChunkById()

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

$this->assertSame(3, $i);
}

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

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

$user->articles()->chunkByIdDesc(1, function (Collection $collection) use (&$i) {
$this->assertEquals(3 - $i, $collection->first()->id);
$i++;
});

$this->assertSame(3, $i);
Expand All @@ -83,9 +99,9 @@ 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'],
['id' => 1, 'title' => 'Another title'],
['id' => 2, 'title' => 'Another title'],
['id' => 3, 'title' => 'Another title'],
]);

$user->articles()->sync([3, 1, 2]);
Expand Down Expand Up @@ -126,10 +142,9 @@ public function articles()

class BelongsToManyChunkByIdTestTestArticle extends Eloquent
{
protected $primaryKey = 'aid';
protected $table = 'articles';
protected $keyType = 'string';
public $incrementing = false;
public $timestamps = false;
protected $fillable = ['aid', 'title'];
protected $fillable = ['id', 'title'];
}
134 changes: 134 additions & 0 deletions tests/Database/DatabaseEloquentBelongsToManyEachByIdTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

namespace Illuminate\Tests\Database;

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

class DatabaseEloquentBelongsToManyEachByIdTest extends TestCase
{
protected function setUp(): void
{
$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('id');
$table->string('title');
});

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

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

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

$user->articles()->eachById(function (BelongsToManyEachByIdTestTestArticle $model) use (&$i) {
$i++;
$this->assertEquals($i, $model->id);
});

$this->assertSame(3, $i);
}

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

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

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

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

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

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

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

class BelongsToManyEachByIdTestTestArticle extends Eloquent
{
protected $table = 'articles';
protected $keyType = 'string';
public $incrementing = false;
public $timestamps = false;
protected $fillable = ['id', 'title'];
}
Loading