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

[8.x] Init the traits when the model is being unserialized #37492

Merged
merged 2 commits into from
May 27, 2021
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: 2 additions & 0 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2014,5 +2014,7 @@ public function __sleep()
public function __wakeup()
{
$this->bootIfNotBooted();

$this->initializeTraits();
}
}
38 changes: 38 additions & 0 deletions tests/Integration/Queue/ModelSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,23 @@ public function testItReloadsNestedRelationships()
$this->assertEquals($nestedUnSerialized->order->getRelations(), $order->getRelations());
}

public function testItCanRunModelBootsAndTraitInitializations()
{
$model = new ModelBootTestWithTraitInitialization();

$this->assertTrue($model->fooBar);
$this->assertTrue($model::hasGlobalScope('foo_bar'));

$model::clearBootedModels();

$this->assertFalse($model::hasGlobalScope('foo_bar'));

$unSerializedModel = unserialize(serialize($model));

$this->assertFalse($unSerializedModel->fooBar);
$this->assertTrue($model::hasGlobalScope('foo_bar'));
}

/**
* Regression test for https://github.com/laravel/framework/issues/23068.
*/
Expand Down Expand Up @@ -319,6 +336,27 @@ public function test_model_serialization_structure()
}
}

trait TraitBootsAndInitializersTest
{
public $fooBar = false;

public function initializeTraitBootsAndInitializersTest()
{
$this->fooBar = ! $this->fooBar;
}

public static function bootTraitBootsAndInitializersTest()
{
static::addGlobalScope('foo_bar', function () {
});
}
}

class ModelBootTestWithTraitInitialization extends Model
{
use TraitBootsAndInitializersTest;
}

class ModelSerializationTestUser extends Model
{
public $table = 'users';
Expand Down