Skip to content

Commit 39fa9f4

Browse files
authored
[12.x] Fix double query in model relation serialization (#55547)
* fix: only load missing relations when restoring model * fix: disable lazy loading for model serialization test to prevent failure * feat: add test for reloading serialized relationships only once
1 parent c1d8da6 commit 39fa9f4

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function restoreModel($value)
107107
{
108108
return $this->getQueryForModelRestoration(
109109
(new $value->class)->setConnection($value->connection), $value->id
110-
)->useWritePdo()->firstOrFail()->load($value->relations ?? []);
110+
)->useWritePdo()->firstOrFail()->loadMissing($value->relations ?? []);
111111
}
112112

113113
/**

tests/Integration/Queue/ModelSerializationTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ protected function setUp(): void
3131
{
3232
parent::setUp();
3333

34+
Model::preventLazyLoading(false);
35+
3436
Schema::create('users', function (Blueprint $table) {
3537
$table->increments('id');
3638
$table->string('email');
@@ -161,6 +163,28 @@ public function testItReloadsRelationships()
161163
$this->assertEquals($unSerialized->order->getRelations(), $order->getRelations());
162164
}
163165

166+
public function testItReloadsRelationshipsOnlyOnce()
167+
{
168+
$order = tap(ModelSerializationTestCustomOrder::create(), function (ModelSerializationTestCustomOrder $order) {
169+
$order->wasRecentlyCreated = false;
170+
});
171+
172+
$product1 = Product::create();
173+
$product2 = Product::create();
174+
175+
Line::create(['order_id' => $order->id, 'product_id' => $product1->id]);
176+
Line::create(['order_id' => $order->id, 'product_id' => $product2->id]);
177+
178+
$order->load('line', 'lines', 'products');
179+
180+
$this->expectsDatabaseQueryCount(4);
181+
182+
$serialized = serialize(new ModelRelationSerializationTestClass($order));
183+
$unSerialized = unserialize($serialized);
184+
185+
$this->assertEquals($unSerialized->order->getRelations(), $order->getRelations());
186+
}
187+
164188
public function testItReloadsNestedRelationships()
165189
{
166190
$order = tap(Order::create(), function (Order $order) {
@@ -433,6 +457,29 @@ public function newCollection(array $models = [])
433457
}
434458
}
435459

460+
class ModelSerializationTestCustomOrder extends Model
461+
{
462+
public $table = 'orders';
463+
public $guarded = [];
464+
public $timestamps = false;
465+
public $with = ['line', 'lines', 'products'];
466+
467+
public function line()
468+
{
469+
return $this->hasOne(Line::class, 'order_id');
470+
}
471+
472+
public function lines()
473+
{
474+
return $this->hasMany(Line::class, 'order_id');
475+
}
476+
477+
public function products()
478+
{
479+
return $this->belongsToMany(Product::class, 'lines', 'order_id');
480+
}
481+
}
482+
436483
class Order extends Model
437484
{
438485
public $guarded = [];

0 commit comments

Comments
 (0)