Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
[5.8] Fixed queue jobs using SerializesModels losing order of passed in collections #29136
The current implementation loses the intended order for passed in collections.
Eg. let's say we have a queue job exporting users sorted by email address.
User::create([ 'email' => 'firstname.lastname@example.org' ]); User::create([ 'email' => 'email@example.com' ]); $users = User::orderBy('name')->get(); // SELECT * FROM users ORDER BY name // [ [ 'id' => 2, 'email' => 'firstname.lastname@example.org ], [ 'id' => 1, 'email' => 'email@example.com' ] ] $serialized = serialize(new Export($users));
Now on unserialization, Laravel doesn't know about the order clause and runs a simple WHERE IN query, which returns the records in the original order as inserted into db.
$unserialized = unserialize($serialized); // SELECT * FROM users WHERE id IN (2, 1) // [ [ 'id' => 1, 'email' => 'firstname.lastname@example.org' ], [ 'id' => 2, 'email' => 'email@example.com ] ]
The proposed fix adds an extra step where the restored collection is re-mapped based on the serialized collection of keys which is in the correct order, thus restoring the intended order.
Included an integration test demonstrating the issue.
Fixed the failing tests by skipping the order recovery step for pivot records.
Kinda share the performance concern, but with collections of tens or hundreds of thousands of items you'd run into memory limitations before cpu imo. The memory footprint shouldn't be too bad since both original and sorted collections are referencing the same objects. But I'm just guessing.
The current fix makes for some awkward code, maybe someone else has a better idea?
Alternatively we could document this as a known limitation as it's definitively an unexpected behaviour atm.