-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
PHPORM-238 Add support for withCount
and other aggregations
#3182
base: 5.x
Are you sure you want to change the base?
Conversation
@@ -556,6 +557,8 @@ public function generateCacheKey() | |||
/** @return ($function is null ? AggregationBuilder : mixed) */ | |||
public function aggregate($function = null, $columns = ['*']) | |||
{ | |||
assert(is_array($columns), new TypeError(sprintf('Argument #2 ($columns) must be of type array, %s given', get_debug_type($columns)))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We cannot add the type to the argument as it's missing in the parent method. When the value is invalid here, the error comes very late in Query\Builder::toMQL
.
src/Query/Builder.php
Outdated
// When the aggregation is per group, we return the results as is. | ||
if ($this->groups) { | ||
return $results; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trying to support this feature in Laravel, so that withCount
on hybrid relations could work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you just not add withCount on hybrid relations, its a blocker. Also a suggestion It would be better for hybrid to be a separate package instead of being integrated into laravel-mongodb if that could ease the maintenance and focus for other mongodb eloquent methods.
Fix PHPORM-238
Implement this feature from Laravel: https://laravel.com/docs/11.x/eloquent-relationships#counting-related-models
Counting relations with SQL is done using a subquery. For MongoDB, there is 2 ways:
$group
by foreign key.$lookup
to load references and count them. The process would be similar for embedded relations (without the$lookup
). But this doesn't work with hybrid relations.Checklist