Skip to content

Conversation

@mattrenner
Copy link
Member

No description provided.

This commit fixes a potential performance penalty that is paid when a one-to-many or many-to-many relation is loaded using `with()` but has no joined rows. In this scenario you effectively lose the benefits of eager loading using `with()`, as the fact the relation is empty is never set, so when accessing it later the ORM dutifully goes off and queries the DB again, setting the relation to empty if there's no rows.

This change means that when a `with()` is used a default value is set (`null` for -to-one and an empty array for -to-many). 

It also exacerbates an edge case where if join or where conditions limit the results returned for relations loaded using `with()` to zero you can't then call the relation (via `->RelationName` or `->getRelationName()`) to get the unfiltered list. 

In theory this is not a defined behaviour anyway, because if any joined rows are returned they remain filtered. I'd argue that this change therefore makes this behaviour consistent regardless of whether the initial query returned joined results or not.
…lidator

Adds a new `with` argument to the `exists` validator that expects an array of relation names to load as part of validation. 

Means its possible to eager load simple relations without needing to dip out into a custom validator. Does not support custom where conditions on relations etc, just attaching relations to an existing filter.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants