Skip to content
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

Constrained eager loading does not detect change to where constraint #295

Open
saernz opened this issue Oct 8, 2019 · 3 comments
Open
Assignees

Comments

@saernz
Copy link
Contributor

saernz commented Oct 8, 2019

Describe the bug
When using constrained eager loading the first cached result is always return regardless of any changes to the constraint. I could not find any other issues relating to this and the docs only mentioned lazy-loaded relationships are unsupported at the moment so I thought it wise to raise this as a bug.

Eloquent Query
I've provided a test that you can copy and paste into tests/Integration/CachedBuilderRelationshipsTest.php which should show my problem.

public function testConstrainedEagerLoads()
{
    factory(Book::class)
        ->create([
            "author_id" => 1,
            "publisher_id" => 1,
            "title" => "Jason Bourne",
        ]);
    factory(Book::class)
        ->create([
            "author_id" => 1,
            "publisher_id" => 1,
            "title" => "Bason Journe",
        ]);

    $jasonBourneBooks = Publisher::with(['books' => function ($q) {
        $q->where('title', 'Jason Bourne');
    }])->get()->pluck('books')->flatten();

    $this->assertCount(1, $jasonBourneBooks);
    $this->assertEquals("Jason Bourne", $jasonBourneBooks->first()->title);

    $basonJournBooks = Publisher::with(['books' => function ($q) {
        $q->where('title', 'Bason Journe');
    }])->get()->pluck('books')->flatten();

    $this->assertCount(1, $basonJournBooks);
    $this->assertEquals("Bason Journe", $basonJournBooks->first()->title);
}

Then when running ./vendor/bin/phpunit --filter CachedBuilderRelationshipsTest I get the following result:

There was 1 failure:

1) GeneaLabs\LaravelModelCaching\Tests\Integration\CachedBuilderRelationshipsTest::testConstrainedEagerLoads
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'Bason Journe'
+'Jason Bourne'

/Users/some-path/laravel-model-caching/tests/Integration/CachedBuilderRelationshipsTest.php:80

FAILURES!
Tests: 3, Assertions: 7, Failures: 1.

Environment

  • PHP: 7.3.9
  • OS: Alpine
  • Laravel: 5.8.19
  • Model Caching: 0.7.0

Additional context
Can use:

$result = app("model-cache")->runDisabled(function () {
    return Publisher::with(['books' => function ($q) {
        $q->where('title', 'Bason Journe');
    }])->get();
});

to workaround the problem for my specific query at the moment, but for some reason ->disableCache() did not work.

Thanks for the great library, has been working great so far. Wish I had more time to look into this more or do a PR but even as it is I have probably got a little too side tracked :)

@mikebronner
Copy link
Owner

@saernz Thanks for this! I will look into this over the next few days, time permitting.

@mikebronner
Copy link
Owner

@saernz Will have to work on this some more. Tried coming up with a solution today, but having a bit of trouble trying to extract the key of with with query. WIP

@mikebronner
Copy link
Owner

Related to #291

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants