-
Notifications
You must be signed in to change notification settings - Fork 11.4k
[12.x] Adds EagerLoad
attribute and initializeOnQueue
hook
#55590
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
Draft
timacdonald
wants to merge
19
commits into
laravel:12.x
Choose a base branch
from
timacdonald:with-relations
base: 12.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
954c2a1
Add WithRelations attribute
timacdonald 2d14b92
rename
timacdonald 211989d
Use loadMissing
timacdonald 1150ce2
Fix name
timacdonald cd19160
Rename test class
timacdonald b524a51
ensure load missing
timacdonald de26570
wip
timacdonald d579cf7
Fill out test
timacdonald a89a861
Add boot on queue tests
timacdonald 434a204
Rename to initializeOnQueue
timacdonald 4a24d3f
Use load
timacdonald 7c7804d
Rename to eager load
timacdonald 6550c65
Improve typing
timacdonald b204894
Improve tests
timacdonald 41eebb5
Improve tests
timacdonald ca646ee
Improve tests
timacdonald f0a2ff7
Limit initialize to Queueable trait
timacdonald faa146a
Use lower-level trait
timacdonald 4596d26
Lint
timacdonald File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Illuminate\Queue\Attributes; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::TARGET_PROPERTY)] | ||
class WithoutRelations | ||
{ | ||
public function __construct(public array $relations) | ||
{ | ||
// | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
use Illuminate\Database\Eloquent\Relations\Pivot; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Queue\Attributes\WithRelations; | ||
use Illuminate\Queue\Attributes\WithoutRelations; | ||
use Illuminate\Queue\SerializesModels; | ||
use LogicException; | ||
|
@@ -374,6 +375,45 @@ public function test_serialization_types_empty_custom_eloquent_collection() | |
|
||
$this->assertTrue(true); | ||
} | ||
|
||
public function test_it_respects_with_relations_attribute_for_models() | ||
{ | ||
$user = User::create([ | ||
'email' => '[email protected]', | ||
]); | ||
|
||
$serialized = serialize(new ModelSerializationWithRelations($user)); | ||
$this->assertSame( | ||
'O:66:"Illuminate\Tests\Integration\Queue\ModelSerializationWithRelations":1:{s:8:"property";O:45:"Illuminate\Contracts\Database\ModelIdentifier":5:{s:5:"class";s:39:"Illuminate\Tests\Integration\Queue\User";s:2:"id";i:1;s:9:"relations";a:0:{}s:10:"connection";s:7:"testing";s:15:"collectionClass";N;}}', | ||
$serialized | ||
); | ||
|
||
/** @var ModelSerializationWithRelations $unserialized */ | ||
$unserialized = unserialize($serialized); | ||
|
||
$this->assertTrue($unserialized->property->relationLoaded('roles')); | ||
} | ||
|
||
public function test_it_respects_with_relations_attribute_for_collections() | ||
{ | ||
$taylor = User::create([ | ||
'email' => '[email protected]', | ||
]); | ||
$tim = User::create([ | ||
'email' => '[email protected]', | ||
]); | ||
|
||
$serialized = serialize(new ModelSerializationWithRelations(new Collection([$taylor, $tim]))); | ||
$this->assertSame( | ||
'O:66:"Illuminate\Tests\Integration\Queue\ModelSerializationWithRelations":1:{s:8:"property";O:45:"Illuminate\Contracts\Database\ModelIdentifier":5:{s:5:"class";s:39:"Illuminate\Tests\Integration\Queue\User";s:2:"id";a:2:{i:0;i:1;i:1;i:2;}s:9:"relations";a:0:{}s:10:"connection";s:7:"testing";s:15:"collectionClass";N;}}', | ||
$serialized, | ||
); | ||
|
||
/** @var ModelSerializationWithRelations $unserialized */ | ||
$unserialized = unserialize($serialized); | ||
|
||
$this->assertTrue($unserialized->property->every->relationLoaded('roles')); | ||
} | ||
} | ||
|
||
trait TraitBootsAndInitializersTest | ||
|
@@ -567,6 +607,18 @@ public function __construct(public User $user, public DataValueObject $value) | |
} | ||
} | ||
|
||
class ModelSerializationWithRelations | ||
{ | ||
use SerializesModels; | ||
|
||
public function __construct( | ||
#[WithRelations(['roles'])] | ||
public $property | ||
) { | ||
// | ||
} | ||
} | ||
|
||
class ModelRelationSerializationTestClass | ||
{ | ||
use SerializesModels; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.