Skip to content

Commit f7c7f10

Browse files
committed
fix & cs
adjustments
1 parent 74811d4 commit f7c7f10

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

src/Illuminate/Database/Eloquent/Concerns/HasEvents.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,12 @@ protected function fireModelEvent($event, $halt = true)
208208
return true;
209209
}
210210

211+
// If we have automatic eager loading enabled for the creating event unset any null relations
212+
// as it can cause issues and set the relations to null even if they exist.
213+
if (static::isAutomaticallyEagerLoadingRelationships() && $event == 'creating') {
214+
$this->setRelations(array_filter($this->getRelations()));
215+
}
216+
211217
// First, we will get the proper method to call on the event dispatcher, and then we
212218
// will attempt to fire a custom, object based event for the given event. If that
213219
// returns a result we can return that result, or we'll call the string events.

tests/Integration/Database/EloquentModelRelationAutoloadTest.php

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Illuminate\Tests\Integration\Database\EloquentModelRelationAutoloadTest;
44

55
use DB;
6+
use Illuminate\Database\Eloquent\Factories\Factory;
7+
use Illuminate\Database\Eloquent\Factories\HasFactory;
68
use Illuminate\Database\Eloquent\Model;
79
use Illuminate\Database\Schema\Blueprint;
810
use Illuminate\Support\Facades\Schema;
@@ -12,6 +14,14 @@ class EloquentModelRelationAutoloadTest extends DatabaseTestCase
1214
{
1315
protected function afterRefreshingDatabase()
1416
{
17+
Schema::create('tags', function (Blueprint $table) {
18+
$table->increments('id');
19+
$table->string('name')->nullable();
20+
$table->string('status')->nullable();
21+
$table->unsignedInteger('post_id')->nullable();
22+
$table->unsignedInteger('video_id')->nullable();
23+
});
24+
1525
Schema::create('posts', function (Blueprint $table) {
1626
$table->increments('id');
1727
});
@@ -214,6 +224,95 @@ public function testRelationAutoloadVariousNestedMorphRelations()
214224

215225
DB::disableQueryLog();
216226
}
227+
228+
public function testDoesntCauseNQueryIssueUsingGet()
229+
{
230+
Model::automaticallyEagerLoadRelationships();
231+
232+
DB::enableQueryLog();
233+
234+
$post = Post::create();
235+
236+
$video = Video::create();
237+
$video2 = Video::create();
238+
$video3 = Video::create();
239+
240+
Tag::create(['post_id' => $post->id, 'video_id' => $video->id]);
241+
Tag::create(['post_id' => $post->id, 'video_id' => $video2->id]);
242+
Tag::create(['post_id' => $post->id, 'video_id' => $video3->id]);
243+
244+
$videos = [];
245+
foreach ($post->tags()->get() as $tag) {
246+
$videos[] = $tag->video;
247+
}
248+
249+
$this->assertCount(12, DB::getQueryLog());
250+
$this->assertCount(3, $videos);
251+
252+
Model::automaticallyEagerLoadRelationships(false);
253+
}
254+
255+
public function testRelationAutoloadWorksOnCreatingEvent()
256+
{
257+
Model::automaticallyEagerLoadRelationships();
258+
259+
DB::enableQueryLog();
260+
261+
$tags = Tag::factory()->times(3)->make();
262+
263+
$post = Post::factory()->create();
264+
265+
$post->tags()->saveMany($tags);
266+
267+
$this->assertCount(7, DB::getQueryLog());
268+
269+
Model::automaticallyEagerLoadRelationships(false);
270+
271+
DB::disableQueryLog();
272+
}
273+
}
274+
275+
class TagFactory extends Factory
276+
{
277+
protected $model = Tag::class;
278+
279+
public function definition()
280+
{
281+
return [];
282+
}
283+
}
284+
285+
class Tag extends Model
286+
{
287+
use HasFactory;
288+
289+
public $timestamps = false;
290+
291+
protected $guarded = [];
292+
293+
protected static function booted()
294+
{
295+
static::creating(function ($model) {
296+
if ($model->post->shouldApplyStatus()) {
297+
$model->status = 'Todo';
298+
}
299+
});
300+
}
301+
302+
protected static function newFactory()
303+
{
304+
return TagFactory::new();
305+
}
306+
307+
public function post()
308+
{
309+
return $this->belongsTo(Post::class);
310+
}
311+
312+
public function video()
313+
{
314+
return $this->belongsTo(Video::class);
315+
}
217316
}
218317

219318
class Comment extends Model
@@ -238,10 +337,26 @@ public function commentable()
238337
}
239338
}
240339

340+
class PostFactory extends Factory
341+
{
342+
protected $model = Post::class;
343+
344+
public function definition()
345+
{
346+
return [];
347+
}
348+
}
241349
class Post extends Model
242350
{
351+
use HasFactory;
352+
243353
public $timestamps = false;
244354

355+
public function shouldApplyStatus()
356+
{
357+
return false;
358+
}
359+
245360
public function comments()
246361
{
247362
return $this->morphMany(Comment::class, 'commentable');
@@ -256,6 +371,16 @@ public function likes()
256371
{
257372
return $this->morphMany(Like::class, 'likeable');
258373
}
374+
375+
protected static function newFactory()
376+
{
377+
return PostFactory::new();
378+
}
379+
380+
public function tags()
381+
{
382+
return $this->hasMany(Tag::class);
383+
}
259384
}
260385

261386
class Video extends Model

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy