Skip to content

[7.x] Fix regressions on find* methods with Arrayable ids #30312

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 2 commits into from
Oct 18, 2019

Conversation

sebdesign
Copy link
Contributor

Ensure Builder::findOrFail with Arrayable throws ModelNotFoundException

I found a regression in #7048 and #19019, where the Eloquent Builder will not throw a ModelNotFoundException when an Arrayable is passed to findOrFail.

In #7048, we are checking if the $ids is an array and we count the results against the number of ids.

But since #19019, the find method also accepts an Arrayable for the ids.

So if an Arrayable is passed, the check is skipped and findOrFail returns the results.

To fix this, we are first checking if the $ids is an Arrayable and we convert the ids to an array before checking the results.

Ensure find* methods on relationships are accepting Arrayable ids

This regression is also observed in #9143, because the find, findMany and findOrFail methods were copied from the Eloquent Builder to the BelongsToMany and HasManyThrough relations, but they did not account for Arrayable ids.

For this reason, we need to convert the passed ids to an array before executing the queries.

This is a potentially breaking change, so this should not be merged in 6.x. The reason is because in 6.x a collection/arrayable of ids will not throw an exception if the number of ids does not match the number of found records. While an array with the same ids would throw an exception:

For example:

<?php

Post::create(['id' => 1]);

// Before (6.x)
Post::findOrFail([1, 2]); // throws an exception
Post::findOrFail(collect([1, 2])); // returns a collection with posts 1 and 2

// After (7.x)
Post::findOrFail([1, 2]); // throws an exception
Post::findOrFail(collect([1, 2])); // throws an exception

I found a regression in laravel#7048 and laravel#19019, where the Eloquent Builder will not throw a `ModelNotFoundException` when an `Arrayable` is passed to `findOrFail`.

In this laravel#7048, we are checking if the `$ids` is an array and we count the results against the number of ids.

But since laravel#19019, the `find` method also accepts an `Arrayable` for the ids.

So if an `Arrayable` is passed, the check is skipped and the method returns the results.

To fix this, we are first checking if the `$ids` is an  `Arrayable` and we convert the ids to an array before checking the results.
…regression in laravel#7048 and laravel#19019 is also observed in laravel#9143, because the `find`,  `findMany` and `findOrFail` methods are copied from the Eloquent Builder to the `BelongsToMany` and `HasManyThrough` relations.For this reason, we need to convert the passed ids to an array before executing the queries.
@sebdesign sebdesign force-pushed the feature/find-arrayable branch from 15185a9 to a7c38cc Compare October 16, 2019 23:29
@sebdesign
Copy link
Contributor Author

sebdesign commented Oct 16, 2019

I also found another bug while tinkering with findOrFail on BelongsToMany relations. The logic for comparing the number of passed ids with the number of results is wrong.

Consider this example:

$post = Post::create();
$john = User::create();
$jane = User::create();

// John read this post yesterday and today
$post->readers()->attach($john, ['read_at' => now()->subDay()]);
$post->readers()->attach($john, ['read_at' => now()]);

// Jane read this post last week
$post->readers()->attach($jane, ['read_at' => now()->subWeek()]);

// Let's grab these readers with their pivot data
$post->readers()->findOrFail([$john->id, $jane->id]);

Since both users have read the post, we should get 3 results, 2 for John and 1 for Jane.
But instead we get a ModelNotFoundException because the number of ids is 2 while the number of results is 3.

Instead of counting and comparing the number of ids and results, we should check that the model keys match with the given ids.

This could be fixed in 6.x but without this PR it would work only for arrays and not for Arrayables.

@driesvints driesvints changed the title Fix regressions on find* methods with Arrayable ids [7.x] Fix regressions on find* methods with Arrayable ids Oct 17, 2019
@taylorotwell taylorotwell merged commit 5b84aab into laravel:master Oct 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants