Skip to content

[11.x] Prevent infinite recursion on touchesParents() for chaperoned models #52883

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

Merged
merged 1 commit into from
Sep 23, 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
18 changes: 10 additions & 8 deletions src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -792,17 +792,19 @@ public function touches($relation)
*/
public function touchOwners()
{
foreach ($this->getTouchedRelations() as $relation) {
$this->$relation()->touch();
$this->withoutRecursion(function () {
foreach ($this->getTouchedRelations() as $relation) {
$this->$relation()->touch();

if ($this->$relation instanceof self) {
$this->$relation->fireModelEvent('saved', false);
if ($this->$relation instanceof self) {
$this->$relation->fireModelEvent('saved', false);

$this->$relation->touchOwners();
} elseif ($this->$relation instanceof Collection) {
$this->$relation->each->touchOwners();
$this->$relation->touchOwners();
} elseif ($this->$relation instanceof Collection) {
$this->$relation->each->touchOwners();
}
}
}
});
}

/**
Expand Down
83 changes: 83 additions & 0 deletions tests/Database/DatabaseEloquentIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ protected function createSchema()
$table->morphs('taggable');
$table->string('taxonomy')->nullable();
});

$this->schema($connection)->create('categories', function ($table) {
$table->increments('id');
$table->string('name');
$table->integer('parent_id')->nullable();
$table->timestamps();
});
}

$this->schema($connection)->create('non_incrementing_users', function ($table) {
Expand Down Expand Up @@ -2182,6 +2189,61 @@ public function testMorphPivotsCanBeRefreshed()
$this->assertSame('primary', $pivot->taxonomy);
}

public function testTouchingChaperonedChildModelUpdatesParentTimestamps()
{
$before = Carbon::now();

$one = EloquentTouchingCategory::create(['id' => 1, 'name' => 'One']);
$two = $one->children()->create(['id' => 2, 'name' => 'Two']);

$this->assertTrue($before->isSameDay($one->updated_at));
$this->assertTrue($before->isSameDay($two->updated_at));

Carbon::setTestNow($future = $before->copy()->addDays(3));

$two->touch();

$this->assertTrue($future->isSameDay($two->fresh()->updated_at), 'It is not touching model own timestamps.');
$this->assertTrue($future->isSameDay($one->fresh()->updated_at), 'It is not touching chaperoned models related timestamps.');
}

public function testTouchingBiDirectionalChaperonedModelUpdatesAllRelatedTimestamps()
{
$before = Carbon::now();

EloquentTouchingCategory::insert([
['id' => 1, 'name' => 'One', 'parent_id' => null, 'created_at' => $before, 'updated_at' => $before],
['id' => 2, 'name' => 'Two', 'parent_id' => 1, 'created_at' => $before, 'updated_at' => $before],
['id' => 3, 'name' => 'Three', 'parent_id' => 1, 'created_at' => $before, 'updated_at' => $before],
['id' => 4, 'name' => 'Four', 'parent_id' => 2, 'created_at' => $before, 'updated_at' => $before],
]);

$one = EloquentTouchingCategory::find(1);
[$two, $three] = $one->children;
[$four] = $two->children;

$this->assertTrue($before->isSameDay($one->updated_at));
$this->assertTrue($before->isSameDay($two->updated_at));
$this->assertTrue($before->isSameDay($three->updated_at));
$this->assertTrue($before->isSameDay($four->updated_at));

Carbon::setTestNow($future = $before->copy()->addDays(3));

// Touch a random model and check that all of the others have been updated
$models = tap([$one, $two, $three, $four], shuffle(...));
$target = array_shift($models);
$target->touch();

$this->assertTrue($future->isSameDay($target->fresh()->updated_at), 'It is not touching model own timestamps.');

while ($next = array_shift($models)) {
$this->assertTrue(
$future->isSameDay($next->fresh()->updated_at),
'It is not touching related models timestamps.'
);
}
}

/**
* Helpers...
*/
Expand Down Expand Up @@ -2486,3 +2548,24 @@ public function post()
return $this->belongsTo(EloquentTouchingPost::class, 'post_id');
}
}

class EloquentTouchingCategory extends Eloquent
{
protected $table = 'categories';
protected $guarded = [];

protected $touches = [
'parent',
'children',
];

public function parent()
{
return $this->belongsTo(EloquentTouchingCategory::class, 'parent_id');
}

public function children()
{
return $this->hasMany(EloquentTouchingCategory::class, 'parent_id')->chaperone();
}
}