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

not taking param into consideration? #67

Closed
shez1983 opened this issue Feb 12, 2018 · 20 comments
Closed

not taking param into consideration? #67

shez1983 opened this issue Feb 12, 2018 · 20 comments

Comments

@shez1983
Copy link

shez1983 commented Feb 12, 2018

So I have a query such as this. and i have an api endpoint categories/id/products..

$collection = ProductVariant::with( 'product.owner' )->whereHas( 'product', function( $q ) use ( $categoryId )
            {
                $q->whereCategoryId($categoryId)->live();
            } )->inRandomOrder()->live(1)

but when i change the id to say 2 or 3.. I get same products.. is this something I have to take care of? or am i doing something wrong?

Environment

Laravel Version: 5.6.2
Laravel Model Caching Package Version: ^0.2.22
PHP Version: * 7.1.6 *
Homestead Version: N/A (using Valet)
Operating System & Version: *Mac OS - El captain - 10.11.6 *

@mikebronner
Copy link
Owner

mikebronner commented Feb 12, 2018

Thanks for reporting this @shez1983 ! I'll take a look as soon as I get back to the office on Tuesday. In the mean time, can you provide the SQL representation of both queries?

@shez1983
Copy link
Author

i'll make sure I update this post before tuesday.. Thanks :)

@mikebronner
Copy link
Owner

If you are able to, please include the stack trace as well. I do have about 30 minutes to look at it right now, if you are able to update the code :)

@mikebronner
Copy link
Owner

Related or same as #66

@mikebronner mikebronner added this to the 0.2.23 milestone Feb 13, 2018
@mikebronner
Copy link
Owner

@shez1983 Are inRandomOrder() and live() query scopes? Can you paste them here as well? Thanks! :)

@mikebronner mikebronner removed this from the 0.2.23 milestone Feb 14, 2018
@shez1983
Copy link
Author

I am sorry for the delay... :(

"select * from `product_variants` where exists (select * from `products` where `product_variants`.`product_id` = `products`.`id` and `category_id` = ? and `status_id` = ? and `show_on_mobile` = ?) and `is_listed` = ? and (`stock` is null or `stock` > ?) and `product_variants`.`deleted_at` is null order by RAND()"

Here is the live Scope

    {
        return $q->whereIsListed(1)->where(function($q)
        {
           return $q->whereNull('stock')->orWhere('stock', '>', 0);
        });
    }

inRandomOrder is an ELOQUENT/Builder function btw..

I am not sure how to include a stack trace because theres no error..

@mikebronner
Copy link
Owner

@shez1983 thanks for the update. In your eloquent query you aren't specifying get()? Was that just a typo, or are you using a different command?

@shez1983
Copy link
Author

yes. as i said i can see the actual products.. if i didnt specify get() i wouldnt have come to the conclusion of my OP.. :)

@mikebronner
Copy link
Owner

mikebronner commented Feb 24, 2018

Hi @shez1983, I am unable to reproduce this problem in version 0.2.37. Can you test again? I have added a test that checks the caching for the whereHas() method. Please let me know how it goes! :)

See test here: https://github.com/GeneaLabs/laravel-model-caching/blob/master/tests/Unit/CachedModelTest.php#L77

@mikebronner
Copy link
Owner

@shez1983 closing this for now, please let me know if you continue to have this problem.

@shez1983
Copy link
Author

shez1983 commented Mar 1, 2018

oops sorry - too busy with work will let you know sometime (by next week)

btw that link https://github.com/GeneaLabs/laravel-model-caching/blob/master/tests/Unit/CachedModelTest.php#L77 doesnt seem to be working :s
@mikebronner

@shez1983
Copy link
Author

shez1983 commented Mar 4, 2018

Thanks for that - seeing your test, it seems you have not understood my problem...
that test is not recreating what i said in my op..

in that test i expected you to cache both

            ->with('author')
            ->whereHas('author', function ($query) {
                $query->whereId(1);
            })
            ->get();

and

            ->with('author')
            ->whereHas('author', function ($query) {
                $query->whereId(2);
            })
            ->get();

and then make sure the results are not the same..

notice the Ids.. are different

@mikebronner
Copy link
Owner

Thanks for clarifying, that indeed was not clear in your initial post. I will try to recreate the problem as you just described.

@mikebronner
Copy link
Owner

@shez1983 I added the following test: d003028

Does this follow your use-case? I was unable to replicate the issue.

@shez1983
Copy link
Author

shez1983 commented Mar 4, 2018

  •    $this->assertNotEmpty($books1->diffKeys($books2)); 
    

surely this should assertEMPTY if there can only be one author for a book? and you are not caching the results...

you are supposed to be looking at cached results (Unless i am missing something) not comparing eloquent/query results...

@mikebronner
Copy link
Owner

mikebronner commented Mar 4, 2018

Your original post was saying that running two different queries actually returns the same information. Do I understand that correctly?

This test does use caching, and it verifies that the results from each cached query are different. That is to say, that the results provided in $books2 are not the same as those in $books1. Since whereHas queries are cached, as verified in the test above this one, it is not necessary to verify that. We only need to make sure that the second query is not pulling the cache from the first query.

Have you actually been able to recreate this issue with the latest version of the package?

@shez1983
Copy link
Author

shez1983 commented Mar 4, 2018

yes thats correct. in your previous test (link you provided:
https://github.com/GeneaLabs/laravel-model-caching/blob/master/tests/Integration/CachedModelTest.php#L77

you were doing

$cachedResults = $this
            ->cache()
            ->tags($tags)
            ->get($key)['value'];
        $this->assertEquals(1, $books->first()->author->id);
        $this->assertEquals(1, $cachedResults->first()->author->id);

so i was expecting something similar in your latest test you did... d003028

anyway i will re-test (i actually tested it again few days after posting to make sure i didnt make an error) & let you know

@mikebronner
Copy link
Owner

Thanks, I look forward to seeing how it works for you! :)

@mikebronner
Copy link
Owner

HI @shez1983, closing this for now. If there are still issues, please go ahead and re-open. Thanks :)

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