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

[5.6] Fix BelongsToMany pivot relation wakeup #23081

Merged
merged 1 commit into from
Feb 8, 2018
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
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ public function getQueueableIds()
*/
public function getQueueableRelations()
{
return $this->isNotEmpty() ? $this->first()->getRelations() : [];
return $this->isNotEmpty() ? $this->first()->getQueueableRelations() : [];
}

/**
Expand Down
8 changes: 5 additions & 3 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1303,11 +1303,13 @@ public function getQueueableRelations()
$relations = [];

foreach ($this->getRelations() as $key => $relation) {
$relations[] = $key;
if (method_exists($this, $key)) {
$relations[] = $key;
}

if ($relation instanceof QueueableCollection) {
foreach ($relation->getQueueableRelations() as $collectionKey => $collectionValue) {
$relations[] = $key.'.'.$collectionKey;
foreach ($relation->getQueueableRelations() as $collectionValue) {
$relations[] = $key.'.'.$collectionValue;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain this change a bit more? Why did you make this change?

Copy link
Contributor Author

@rodrigopedra rodrigopedra Feb 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As in the Collection I am returning the result of getQueueableRelations instead of the previous getRelations it returns an integer-indexed array with the name of the relations from the first model in the collection., so I used the values instead of the keys.

}

Expand Down
17 changes: 13 additions & 4 deletions tests/Integration/Queue/ModelSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,17 @@ public function it_fails_if_models_on_multi_connections()
*/
public function it_reloads_relationships()
{
$order = Order::create();
$order = tap(Order::create(), function (Order $order) {
$order->wasRecentlyCreated = false;
});

$product1 = Product::create();
$product2 = Product::create();

Line::create(['order_id' => $order->id, 'product_id' => $product1->id]);
Line::create(['order_id' => $order->id, 'product_id' => $product2->id]);

$order->load('line', 'lines');
$order->load('line', 'lines', 'products');

$serialized = serialize(new ModelRelationSerializationTestClass($order));
$unSerialized = unserialize($serialized);
Expand All @@ -167,15 +169,17 @@ public function it_reloads_relationships()
*/
public function it_reloads_nested_relationships()
{
$order = Order::create();
$order = tap(Order::create(), function (Order $order) {
$order->wasRecentlyCreated = false;
});

$product1 = Product::create();
$product2 = Product::create();

Line::create(['order_id' => $order->id, 'product_id' => $product1->id]);
Line::create(['order_id' => $order->id, 'product_id' => $product2->id]);

$order->load('line.product', 'lines', 'lines.product');
$order->load('line.product', 'lines', 'lines.product', 'products');

$nestedSerialized = serialize(new ModelRelationSerializationTestClass($order));
$nestedUnSerialized = unserialize($nestedSerialized);
Expand Down Expand Up @@ -215,6 +219,11 @@ public function lines()
{
return $this->hasMany(Line::class);
}

public function products()
{
return $this->belongsToMany(Product::class, 'lines');
}
}

class Line extends Model
Expand Down