Add support for Laravel-style relations#167
Merged
LukeTowers merged 66 commits intodevelopfrom Oct 9, 2024
Merged
Conversation
We shouldn't test completely third-party code.
We no longer extend this class, and it doesn't seem to provide any functionality anyway.
This method is strictly a relationship method and should be grouped with the other relationship methods.
In order to maintain some semblance of parity with Laravel, we'll now use Laravel's relation constructors and simply overwrite the "new" relation methods to create Winter's relation instances. Winter's relation name and defined constraints functionality will now be called in our own handler after the relation has been instantiated. This will allow us to review the relation directly before any Winter code runs.
This allows people using the relation method format to define the "delete" attribute available to relation definition arrays.
The "validateRelationArgs" method does a whole lot of nothing - the most it does is enforce required parameters. These can be picked up in Laravel anyway.
Added the ability to define the field name and use this as a constraint for attachment relations, separating it from the relation name.
The "Test" suffix might inadvertently be picked up by PHPUnit as a test case.
LukeTowers
reviewed
Aug 16, 2024
LukeTowers
requested changes
Aug 16, 2024
Member
LukeTowers
left a comment
There was a problem hiding this comment.
Still have performance issues with the softdelete trait
Instead of extending the Relation class, we will make the soft delete methods available immediately to the relations. However, you can only *enable* soft delete if the related model uses the soft delete trait, thereby ensuring the correct soft delete functionality is available to the model when required.
Member
Author
|
@LukeTowers I've changed the soft delete methods on the relation to be available at all times, but the actual soft delete flag will only be enabled when |
LukeTowers
reviewed
Sep 26, 2024
LukeTowers
reviewed
Sep 26, 2024
LukeTowers
approved these changes
Sep 26, 2024
Member
|
@bennothommo is there a related docs PR? |
Member
Author
|
@LukeTowers yep, here: wintercms/docs#176 |
…rove performance To prevent a particular infinite loop, we need to define the Relation type when using the Relation attribute for a method so that we don't have to instantiate the relation in order to find out what type it is.
Member
Author
|
Late change: Using the So instead of: #[Relation]It would now be: #[Relation('hasOne')] |
When a relation is defined in both the relation properties and as a method with the same name, an exception will be thrown.
LukeTowers
reviewed
Oct 8, 2024
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR allows people to define Laravel-style relations using methods that return
Relationobjects. So instead of:You may use:
The main reason for this is to reduce friction for porting Laravel code over to Winter, and as a preference point for people who may wish to use the more declarative style of defining relations.
I've adjusted the unit tests to demonstrate this in action, and will be porting over the relation tests in the Winter
systemmodule into Storm, as they really should be in Storm anyway.In order to maintain parity with the array-style configuration of relations in Winter, method-defined relations will need to either implement one of the following to indicate that they provide relations, and allow certain functionality to find them as required (for example, deleting related models when deleting a primary model):
Relationclass (like with the example above)Winter\Storm\Database\Attributes\Relationattribute and define the relation type, like so:Todo