-
Notifications
You must be signed in to change notification settings - Fork 11k
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
[9.x] Eloquent Strict Loading #37334
Closed
Closed
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2da90dc
add connection strictLoadEloquent
themsaid 0a2fc8a
change attribute name
themsaid 6cd08b5
fix tests
themsaid 00a539c
fix style
themsaid 2e2bdaf
fix stykle
themsaid 82d883d
Merge branch 'master' of github.com:laravel/framework into eloquentSt…
themsaid dba90c6
fix typo
themsaid 466569b
configure strictLoading while hydrating
themsaid 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 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 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 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 | ||||
---|---|---|---|---|---|---|
|
@@ -81,6 +81,13 @@ abstract class Model implements Arrayable, ArrayAccess, Jsonable, JsonSerializab | |||||
*/ | ||||||
protected $withCount = []; | ||||||
|
||||||
/** | ||||||
* Indicates whether the lazy loading should be prevented. | ||||||
* | ||||||
* @var bool | ||||||
*/ | ||||||
public $strictLoading; | ||||||
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.
Suggested change
|
||||||
|
||||||
/** | ||||||
* The number of models to return for pagination. | ||||||
* | ||||||
|
39 changes: 39 additions & 0 deletions
39
src/Illuminate/Database/StrictLoadingViolationException.php
This file contains 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,39 @@ | ||
<?php | ||
|
||
namespace Illuminate\Database; | ||
|
||
use RuntimeException; | ||
|
||
class StrictLoadingViolationException extends RuntimeException | ||
{ | ||
/** | ||
* The name of the affected Eloquent model. | ||
* | ||
* @var string | ||
*/ | ||
public $model; | ||
|
||
/** | ||
* The name of the relation. | ||
* | ||
* @var string | ||
*/ | ||
public $relation; | ||
|
||
/** | ||
* Create a new exception instance. | ||
* | ||
* @param object $model | ||
* @param string $relation | ||
* @return static | ||
*/ | ||
public function __construct($model, $relation) | ||
{ | ||
$class = get_class($model); | ||
|
||
parent::__construct("Trying to lazy load [{$relation}] in model [{$class}] is restricted."); | ||
|
||
$this->model = $class; | ||
$this->relation = $relation; | ||
} | ||
} |
This file contains 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
136 changes: 136 additions & 0 deletions
136
tests/Integration/Database/EloquentStrictLoadingTest.php
This file contains 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,136 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Database; | ||
|
||
use Illuminate\Database\Eloquent\Collection; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\StrictLoadingViolationException; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
/** | ||
* @group integration | ||
*/ | ||
class EloquentStrictLoadingTest extends DatabaseTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
Schema::create('test_model1', function (Blueprint $table) { | ||
$table->increments('id'); | ||
}); | ||
|
||
Schema::create('test_model2', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->foreignId('model_1_id'); | ||
}); | ||
|
||
Schema::create('test_model3', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->foreignId('model_2_id'); | ||
}); | ||
} | ||
|
||
protected function getEnvironmentSetUp($app) | ||
{ | ||
$app['config']->set('app.debug', 'true'); | ||
|
||
$app['config']->set('database.default', 'testbench'); | ||
|
||
$app['config']->set('database.connections.testbench', [ | ||
'driver' => 'sqlite', | ||
'database' => ':memory:', | ||
'prefix' => '', | ||
'strict_load' => true, | ||
]); | ||
} | ||
|
||
public function testStrictModeThrowsAnExceptionOnLazyLoading() | ||
{ | ||
$this->expectException(StrictLoadingViolationException::class); | ||
$this->expectExceptionMessage('Trying to lazy load [modelTwos] in model [Illuminate\Tests\Integration\Database\EloquentStrictLoadingTestModel1] is restricted'); | ||
|
||
EloquentStrictLoadingTestModel1::create(); | ||
EloquentStrictLoadingTestModel1::create(); | ||
|
||
$models = EloquentStrictLoadingTestModel1::get(); | ||
|
||
$models[0]->modelTwos; | ||
} | ||
|
||
public function testStrictModeDoesntThrowAnExceptionOnEagerLoading() | ||
{ | ||
$this->app['config']->set('database.connections.testbench.zxc', false); | ||
|
||
EloquentStrictLoadingTestModel1::create(); | ||
EloquentStrictLoadingTestModel1::create(); | ||
|
||
$models = EloquentStrictLoadingTestModel1::with('modelTwos')->get(); | ||
|
||
$this->assertInstanceOf(Collection::class, $models[0]->modelTwos); | ||
} | ||
|
||
public function testStrictModeDoesntThrowAnExceptionOnLazyEagerLoading() | ||
{ | ||
EloquentStrictLoadingTestModel1::create(); | ||
EloquentStrictLoadingTestModel1::create(); | ||
|
||
$models = EloquentStrictLoadingTestModel1::get(); | ||
|
||
$models->load('modelTwos'); | ||
|
||
$this->assertInstanceOf(Collection::class, $models[0]->modelTwos); | ||
} | ||
|
||
public function testStrictModeDoesntThrowAnExceptionOnSingleModelLoading() | ||
{ | ||
$model = EloquentStrictLoadingTestModel1::create(); | ||
|
||
$this->assertInstanceOf(Collection::class, $model->modelTwos); | ||
} | ||
|
||
public function testStrictModeThrowsAnExceptionOnLazyLoadingInRelations() | ||
{ | ||
$this->expectException(StrictLoadingViolationException::class); | ||
$this->expectExceptionMessage('Trying to lazy load [modelThrees] in model [Illuminate\Tests\Integration\Database\EloquentStrictLoadingTestModel2] is restricted'); | ||
|
||
$model1 = EloquentStrictLoadingTestModel1::create(); | ||
EloquentStrictLoadingTestModel2::create(['model_1_id' => $model1->id]); | ||
|
||
$models = EloquentStrictLoadingTestModel1::with('modelTwos')->get(); | ||
|
||
$models[0]->modelTwos[0]->modelThrees; | ||
} | ||
} | ||
|
||
class EloquentStrictLoadingTestModel1 extends Model | ||
{ | ||
public $table = 'test_model1'; | ||
public $timestamps = false; | ||
protected $guarded = []; | ||
|
||
public function modelTwos() | ||
{ | ||
return $this->hasMany(EloquentStrictLoadingTestModel2::class, 'model_1_id'); | ||
} | ||
} | ||
|
||
class EloquentStrictLoadingTestModel2 extends Model | ||
{ | ||
public $table = 'test_model2'; | ||
public $timestamps = false; | ||
protected $guarded = []; | ||
|
||
public function modelThrees() | ||
{ | ||
return $this->hasMany(EloquentStrictLoadingTestModel3::class, 'model_2_id'); | ||
} | ||
} | ||
|
||
class EloquentStrictLoadingTestModel3 extends Model | ||
{ | ||
public $table = 'test_model3'; | ||
public $timestamps = false; | ||
protected $guarded = []; | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid adding a new loop, would it be possible to set the
strictLoading
property when hydrating models? Something like:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but array map is not a loop?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is, but it is already present in the Laravel codebase