Skip to content

Commit

Permalink
Fix lazy loading with limit
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed Jan 4, 2019
1 parent 1618a63 commit bcda46a
Show file tree
Hide file tree
Showing 11 changed files with 234 additions and 72 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -10,7 +10,7 @@
],
"require": {
"php": ">=7.0",
"illuminate/database": "~5.5.29|5.6.*|5.7.*",
"illuminate/database": "~5.5.43|~5.6.34|5.7.*",
"illuminate/support": "^5.5.14"
},
"require-dev": {
Expand Down
6 changes: 5 additions & 1 deletion src/Relations/HasLimit.php
Expand Up @@ -23,7 +23,11 @@ public function take($value)
*/
public function limit($value)
{
$this->query->groupLimit($value, $this->getExistenceCompareKey());
if ($this->parent->exists) {
$this->query->limit($value);
} else {
$this->query->groupLimit($value, $this->getExistenceCompareKey());
}

return $this;
}
Expand Down
6 changes: 5 additions & 1 deletion src/Relations/HasManyThrough.php
Expand Up @@ -16,7 +16,11 @@ class HasManyThrough extends Base
*/
public function limit($value)
{
$this->query->groupLimit($value, $this->getQualifiedFirstKeyName());
if ($this->farParent->exists) {
$this->query->limit($value);
} else {
$this->query->groupLimit($value, $this->getQualifiedFirstKeyName());
}

return $this;
}
Expand Down
33 changes: 33 additions & 0 deletions tests/BelongsToManyTest.php
@@ -0,0 +1,33 @@
<?php

namespace Tests;

use Tests\Models\User;

class BelongsToManyTest extends TestCase
{
public function testLazyLoading()
{
$roles = User::first()->roles()->offset(1)->get();

$this->assertEquals([2, 1], $roles->pluck('id')->all());
}

public function testEagerLoading()
{
$users = User::with('roles')->get();

$this->assertEquals([3, 2], $users[0]->roles->pluck('id')->all());
$this->assertEquals([6, 5], $users[1]->roles->pluck('id')->all());
$this->assertArrayNotHasKey('@laravel_partition := `pivot_user_id`', $users[0]->roles[0]);
}

public function testLazyEagerLoading()
{
$users = User::all()->load('roles');

$this->assertEquals([3, 2], $users[0]->roles->pluck('id')->all());
$this->assertEquals([6, 5], $users[1]->roles->pluck('id')->all());
$this->assertArrayNotHasKey('@laravel_partition := `pivot_user_id`', $users[0]->roles[0]);
}
}
69 changes: 0 additions & 69 deletions tests/HasEagerLimitTest.php

This file was deleted.

35 changes: 35 additions & 0 deletions tests/HasManyTest.php
@@ -0,0 +1,35 @@
<?php

namespace Tests;

use Tests\Models\User;

class HasManyTest extends TestCase
{
public function testLazyLoading()
{
$posts = User::first()->posts()->offset(1)->get();

$this->assertEquals([2, 1], $posts->pluck('id')->all());
}

public function testEagerLoading()
{
$users = User::with('posts')->get();

$this->assertEquals([3, 2], $users[0]->posts->pluck('id')->all());
$this->assertEquals([6, 5], $users[1]->posts->pluck('id')->all());
$this->assertArrayNotHasKey('laravel_row', $users[0]->post);
$this->assertArrayNotHasKey('@laravel_partition := `user_id`', $users[0]->post);
}

public function testLazyEagerLoading()
{
$users = User::all()->load('posts');

$this->assertEquals([3, 2], $users[0]->posts->pluck('id')->all());
$this->assertEquals([6, 5], $users[1]->posts->pluck('id')->all());
$this->assertArrayNotHasKey('laravel_row', $users[0]->post);
$this->assertArrayNotHasKey('@laravel_partition := `user_id`', $users[0]->post);
}
}
31 changes: 31 additions & 0 deletions tests/HasManyThroughTest.php
@@ -0,0 +1,31 @@
<?php

namespace Tests;

use Tests\Models\Country;

class HasManyThroughTest extends TestCase
{
public function testLazyLoading()
{
$posts = Country::first()->posts()->offset(1)->get();

$this->assertEquals([2, 1], $posts->pluck('id')->all());
}

public function testEagerLoading()
{
$countries = Country::with('posts')->get();

$this->assertEquals([3, 2], $countries[0]->posts->pluck('id')->all());
$this->assertEquals([6, 5], $countries[1]->posts->pluck('id')->all());
}

public function testLazyEagerLoading()
{
$countries = Country::all()->load('posts');

$this->assertEquals([3, 2], $countries[0]->posts->pluck('id')->all());
$this->assertEquals([6, 5], $countries[1]->posts->pluck('id')->all());
}
}
31 changes: 31 additions & 0 deletions tests/HasOneTest.php
@@ -0,0 +1,31 @@
<?php

namespace Tests;

use Tests\Models\User;

class HasOneTest extends TestCase
{
public function testLazyLoading()
{
$post = User::first()->post()->offset(1)->first();

$this->assertEquals(2, $post->id);
}

public function testEagerLoading()
{
$users = User::with('post')->get();

$this->assertEquals(3, $users[0]->post->id);
$this->assertEquals(6, $users[1]->post->id);
}

public function testLazyEagerLoading()
{
$users = User::all()->load('post');

$this->assertEquals(3, $users[0]->post->id);
$this->assertEquals(6, $users[1]->post->id);
}
}
31 changes: 31 additions & 0 deletions tests/MorphManyTest.php
@@ -0,0 +1,31 @@
<?php

namespace Tests;

use Tests\Models\Post;

class MorphManyTest extends TestCase
{
public function testLazyLoading()
{
$comments = Post::first()->comments()->offset(1)->get();

$this->assertEquals([2, 1], $comments->pluck('id')->all());
}

public function testEagerLoading()
{
$posts = Post::with('comments')->get();

$this->assertEquals([3, 2], $posts[0]->comments->pluck('id')->all());
$this->assertEquals([6, 5], $posts[1]->comments->pluck('id')->all());
}

public function testLazyEagerLoading()
{
$posts = Post::all()->load('comments');

$this->assertEquals([3, 2], $posts[0]->comments->pluck('id')->all());
$this->assertEquals([6, 5], $posts[1]->comments->pluck('id')->all());
}
}
31 changes: 31 additions & 0 deletions tests/MorphOneTest.php
@@ -0,0 +1,31 @@
<?php

namespace Tests;

use Tests\Models\Post;

class MorphOneTest extends TestCase
{
public function testLazyLoading()
{
$comment = Post::first()->comment()->offset(1)->first();

$this->assertEquals(2, $comment->id);
}

public function testEagerLoading()
{
$posts = Post::with('comment')->get();

$this->assertEquals(3, $posts[0]->comment->id);
$this->assertEquals(6, $posts[1]->comment->id);
}

public function testLazyEagerLoading()
{
$posts = Post::all()->load('comment');

$this->assertEquals(3, $posts[0]->comment->id);
$this->assertEquals(6, $posts[1]->comment->id);
}
}
31 changes: 31 additions & 0 deletions tests/MorphToManyTest.php
@@ -0,0 +1,31 @@
<?php

namespace Tests;

use Tests\Models\Post;

class MorphToManyTest extends TestCase
{
public function testLazyLoading()
{
$tags = Post::first()->tags()->offset(1)->get();

$this->assertEquals([2, 1], $tags->pluck('id')->all());
}

public function testEagerLoading()
{
$posts = Post::with('tags')->get();

$this->assertEquals([3, 2], $posts[0]->tags->pluck('id')->all());
$this->assertEquals([6, 5], $posts[1]->tags->pluck('id')->all());
}

public function testLazyEagerLoading()
{
$posts = Post::all()->load('tags');

$this->assertEquals([3, 2], $posts[0]->tags->pluck('id')->all());
$this->assertEquals([6, 5], $posts[1]->tags->pluck('id')->all());
}
}

0 comments on commit bcda46a

Please sign in to comment.