-
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 all commits
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,17 @@ | ||
<?php | ||
|
||
namespace Illuminate\Queue\Attributes; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::TARGET_PROPERTY)] | ||
class EagerLoad | ||
{ | ||
/** | ||
* @param list<string> $relations | ||
*/ | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,10 @@ | |
|
||
namespace Illuminate\Queue; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Database\Eloquent\Collection; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Queue\Attributes\EagerLoad; | ||
use Illuminate\Queue\Attributes\WithoutRelations; | ||
use ReflectionClass; | ||
use ReflectionProperty; | ||
|
@@ -89,9 +93,22 @@ public function __unserialize(array $values) | |
continue; | ||
} | ||
|
||
$property->setValue( | ||
$this, $this->getRestoredPropertyValue($values[$name]) | ||
); | ||
$value = $this->getRestoredPropertyValue($values[$name]); | ||
|
||
$property->setValue($this, $value); | ||
|
||
if ( | ||
($value instanceof Model || $value instanceof Collection) && | ||
($attribute = $property->getAttributes(EagerLoad::class)[0] ?? null) | ||
) { | ||
$relations = $attribute->getArguments()[0]; | ||
|
||
$value->load($relations); | ||
} | ||
} | ||
|
||
if (in_array(Queueable::class, class_uses_recursive($this)) && method_exists($this, 'initializeOnQueue')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like swapping the order would be more optimal, but probably not by much: if (method_exists($this, 'initializeOnQueue') && in_array(Queueable::class, class_uses_recursive($this))) { Probably would only be important if this was called a lot. |
||
$this->initializeOnQueue(); | ||
} | ||
} | ||
|
||
|
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 |
---|---|---|
|
@@ -5,10 +5,14 @@ | |
use Illuminate\Database\Eloquent\Collection; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\Pivot; | ||
use Illuminate\Database\Events\QueryExecuted; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Foundation\Queue\Queueable; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Queue\Attributes\EagerLoad; | ||
use Illuminate\Queue\Attributes\WithoutRelations; | ||
use Illuminate\Queue\SerializesModels; | ||
use Illuminate\Support\Facades\DB; | ||
use LogicException; | ||
use Orchestra\Testbench\Attributes\WithConfig; | ||
use Orchestra\Testbench\TestCase; | ||
|
@@ -374,6 +378,109 @@ public function test_serialization_types_empty_custom_eloquent_collection() | |
|
||
$this->assertTrue(true); | ||
} | ||
|
||
public function test_it_can_eagerly_load_relationships_on_models() | ||
{ | ||
$user = User::create([ | ||
'email' => '[email protected]', | ||
]); | ||
|
||
$serialize = serialize(new ModelSerializationEagerLoadRoles($user)); | ||
|
||
$queries = []; | ||
DB::listen(function (QueryExecuted $query) use (&$queries) { | ||
$queries[] = $query; | ||
}); | ||
|
||
$unserialized = unserialize($serialize); | ||
|
||
$this->assertFalse($user->relationLoaded('roles')); | ||
$this->assertTrue($unserialized->value->relationLoaded('roles')); | ||
$this->assertCount(2, $queries); | ||
} | ||
|
||
public function test_it_can_eagerly_load_relationships_on_collections() | ||
{ | ||
$users = new Collection([ | ||
User::create(['email' => '[email protected]']), | ||
User::create(['email' => '[email protected]']), | ||
]); | ||
|
||
$serialize = serialize(new ModelSerializationEagerLoadRoles($users)); | ||
|
||
$queries = []; | ||
DB::listen(function (QueryExecuted $query) use (&$queries) { | ||
$queries[] = $query; | ||
}); | ||
|
||
$unserialized = unserialize($serialize); | ||
|
||
$this->assertTrue($unserialized->value->every->relationLoaded('roles')); | ||
$this->assertFalse($users->some->relationLoaded('roles')); | ||
$this->assertCount(2, $queries); | ||
} | ||
|
||
public function test_it_eager_loads_already_loaded_relationships() | ||
{ | ||
$user = User::create([ | ||
'email' => '[email protected]', | ||
]); | ||
$user->roles()->create(); | ||
$user->load('roles'); | ||
|
||
$serialized = serialize(new ModelSerializationEagerLoadRoles($user)); | ||
|
||
// create a role while the value is serialized... | ||
$user->roles()->create(); | ||
|
||
$queries = []; | ||
DB::listen(function (QueryExecuted $query) use (&$queries) { | ||
$queries[] = $query; | ||
}); | ||
$unserialized = unserialize($serialized); | ||
|
||
$this->assertTrue($unserialized->value->relationLoaded('roles')); | ||
$this->assertTrue($user->relationLoaded('roles')); | ||
$this->assertCount(2, $unserialized->value->roles); | ||
$this->assertCount(1, $user->roles); | ||
$this->assertCount(3, $queries); | ||
} | ||
|
||
public function test_it_can_mix_without_relations_and_eager_load() | ||
{ | ||
$order = Order::create()->load('products'); | ||
|
||
$serialize = serialize(new ModelSerializationWithoutRelationsAndEagerLoadLines($order)); | ||
|
||
$queries = []; | ||
DB::listen(function (QueryExecuted $query) use (&$queries) { | ||
$queries[] = $query; | ||
}); | ||
|
||
$unserialized = unserialize($serialize); | ||
|
||
$this->assertTrue($unserialized->value->relationLoaded('lines')); | ||
$this->assertTrue(! $unserialized->value->relationLoaded('products')); | ||
$this->assertCount(2, $queries); | ||
} | ||
|
||
public function test_it_initilize_on_the_queue() | ||
{ | ||
$order = Order::create(); | ||
|
||
$serialized = serialize(new ModelSerializationWithInitilizeOnQueueHook($order)); | ||
|
||
$queries = []; | ||
DB::listen(function (QueryExecuted $query) use (&$queries) { | ||
$queries[] = $query; | ||
}); | ||
|
||
$unserialized = unserialize($serialized); | ||
|
||
$this->assertTrue($unserialized->value->relationLoaded('lines')); | ||
$this->assertTrue(! $unserialized->value->relationLoaded('products')); | ||
$this->assertCount(2, $queries); | ||
} | ||
} | ||
|
||
trait TraitBootsAndInitializersTest | ||
|
@@ -567,6 +674,49 @@ public function __construct(public User $user, public DataValueObject $value) | |
} | ||
} | ||
|
||
class ModelSerializationEagerLoadRoles | ||
{ | ||
use SerializesModels; | ||
|
||
public function __construct( | ||
#[EagerLoad(['roles'])] | ||
public $value, | ||
) { | ||
// | ||
} | ||
} | ||
|
||
#[WithoutRelations] | ||
class ModelSerializationWithoutRelationsAndEagerLoadLines | ||
{ | ||
use SerializesModels; | ||
|
||
public function __construct( | ||
#[EagerLoad(['lines'])] | ||
public $value | ||
) { | ||
// | ||
} | ||
} | ||
|
||
class ModelSerializationWithInitilizeOnQueueHook | ||
{ | ||
use Queueable; | ||
|
||
public function __construct( | ||
public $value, | ||
) { | ||
// | ||
} | ||
|
||
protected function initializeOnQueue() | ||
{ | ||
$this->value->load([ | ||
'lines' => fn ($q) => $q->where('product_id', 5), | ||
]); | ||
} | ||
} | ||
|
||
class ModelRelationSerializationTestClass | ||
{ | ||
use SerializesModels; | ||
|
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.