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

Apply conditions/scopes to related model query #536

Merged
merged 26 commits into from
Nov 20, 2020
Merged

Apply conditions/scopes to related model query #536

merged 26 commits into from
Nov 20, 2020

Conversation

mjauvin
Copy link
Contributor

@mjauvin mjauvin commented Oct 26, 2020

Fixes: octobercms/october#4952

This overrides the following method in Laravel:

https://github.com/laravel/framework/blob/8.x/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php#L83

Specifically replaces the following lines to fetch the records that actually belongs to the relation (using the conditions/scopes of the relation):

https://github.com/laravel/framework/blob/8.x/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php#L92-L93

@mjauvin
Copy link
Contributor Author

mjauvin commented Oct 26, 2020

@Flynsarmy can you confirm this fixes your problem?

@mjauvin mjauvin changed the title override Laravel Relation sync() method Override Laravel Relation sync() method Oct 27, 2020
@Flynsarmy
Copy link
Contributor

Flynsarmy commented Oct 27, 2020

This PR helps but there still appears to be a bug which you can replicate with my test plugin in octobercms/october#4952. Uncheck both Tag checkboxes but leave one or more Category checkboxes checked. Hit save and refresh. You'll notice the categories did not save correctly. A look in the DB confirms no categories were saved to the flynsarmy_relationtest_posts_terms table.

@mjauvin
Copy link
Contributor Author

mjauvin commented Oct 28, 2020

@Flynsarmy try this, I think I fixed the remaining bug.

@mjauvin mjauvin changed the title Override Laravel Relation sync() method Apply conditions/scopes to related model Oct 28, 2020
@mjauvin mjauvin changed the title Apply conditions/scopes to related model Apply conditions/scopes to related model query Oct 28, 2020
@Flynsarmy
Copy link
Contributor

@mjauvin Looks good! Fixed.

@mjauvin
Copy link
Contributor Author

mjauvin commented Oct 29, 2020

I and @Flynsarmy have tested this as a working fix.

@LukeTowers
Copy link
Contributor

A unit test for the original issue would be helpful here @mjauvin

@mjauvin
Copy link
Contributor Author

mjauvin commented Nov 2, 2020

@LukeTowers there are tests in october/october for testing relations, would that make sense to improve on those tests instead of rewriting these in october/library?

@LukeTowers
Copy link
Contributor

Might be, any thoughts @bennothommo?

@bennothommo
Copy link
Contributor

bennothommo commented Nov 3, 2020

@mjauvin @LukeTowers the test should really exist in here if the changed functionality is here - while it's likely that 99.9% of the time, this will be used in October CMS, it's still proper testing procedure for this sort of functionality to be tested in isolation.

I would simply port a couple of those relation tests back into this library and adjust as necessary.

@LukeTowers
Copy link
Contributor

@bennothommo I think we might need your skills to setup database tests for this library then, I was having a heck of time trying to make it work for the guarded tests

@mjauvin
Copy link
Contributor Author

mjauvin commented Nov 3, 2020

@LukeTowers I found this:
https://github.com/octobercms/library/blob/1.1/tests/Database/Traits/EncryptableTest.php

    public function setUp(): void
    {
        $capsule = new Illuminate\Database\Capsule\Manager;
        $capsule->addConnection([
            'driver'   => 'sqlite',
            'database' => ':memory:',
            'prefix'   => ''
        ]);

        $capsule->setAsGlobal();
        $capsule->bootEloquent();

        $capsule->schema()->create('secrets', function ($table) {
            $table->increments('id');
            $table->string('secret');
            $table->timestamps();
        });
}

That should help

@bennothommo
Copy link
Contributor

@LukeTowers what @mjauvin posted is the only way of testing database functions that I'm aware of with the library. Give that a try and let me know if it doesn't work - I'll have to port some of the functionality from October CMS back into the library.

I swear I did this originally with the L6 development, but had it reversed afterwards - c'est la vie :(.

@mjauvin
Copy link
Contributor Author

mjauvin commented Nov 6, 2020

Should be good now!

tests/DbTestCase.php Outdated Show resolved Hide resolved
tests/Database/RelationsTest.php Outdated Show resolved Hide resolved
@bennothommo
Copy link
Contributor

@mjauvin Just to eliminate any doubt, would you mind adding a couple more scenarios in the tests, namely:

  • Syncing multiple records
  • Detaching one record when multiple are attached

If those are covered, I'm happy to merge.

@bennothommo
Copy link
Contributor

Also, I'm going to merge your DbTest changes manually into develop, as I need it for Luke's PR. When you do the above, you'll need to merge develop back into your branch - hopefully, it won't conflict but if it does, just accept any changes from develop.

bennothommo added a commit that referenced this pull request Nov 10, 2020
@mjauvin
Copy link
Contributor Author

mjauvin commented Nov 10, 2020

The tests have been added @bennothommo

@mjauvin
Copy link
Contributor Author

mjauvin commented Nov 17, 2020

Anything else I need to do to here?

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

Successfully merging this pull request may close these issues.

Relation conditions aren't being applied when saving models
4 participants