Skip to content

Commit

Permalink
Validate arg type and avoid subsequent error
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Oct 17, 2024
1 parent 62a4ff8 commit b72017d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public function eagerLoadRelations(array $models)
->where($withAggregate['constraints'])
->whereIn($withAggregate['relation']->getForeignKeyName(), $modelIds)
->groupBy($withAggregate['relation']->getForeignKeyName())
->aggregate($withAggregate['function'], $withAggregate['column'] ?? [$withAggregate['relation']->getPrimaryKeyName()]);
->aggregate($withAggregate['function'], [$withAggregate['column']]);

foreach ($models as $model) {
$value = $withAggregate['function'] === 'count' ? 0 : null;
Expand Down
11 changes: 7 additions & 4 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Override;
use RuntimeException;
use stdClass;
use TypeError;

use function array_fill_keys;
use function array_is_list;
Expand Down Expand Up @@ -335,7 +336,7 @@ public function toMql(): array
if ($this->aggregate) {
$function = $this->aggregate['function'];

foreach ((array) $this->aggregate['columns'] as $column) {
foreach ($this->aggregate['columns'] as $column) {
// Add unwind if a subdocument array should be aggregated
// column: subarray.price => {$unwind: '$subarray'}
$splitColumns = explode('.*.', $column);
Expand All @@ -344,7 +345,7 @@ public function toMql(): array
$column = implode('.', $splitColumns);
}

$aggregations = blank($this->aggregate['columns']) ? [] : (array) $this->aggregate['columns'];
$aggregations = blank($this->aggregate['columns']) ? [] : $this->aggregate['columns'];

if (in_array('*', $aggregations) && $function === 'count' && empty($group['_id'])) {
$options = $this->inheritConnectionOptions($this->options);
Expand Down Expand Up @@ -484,11 +485,11 @@ public function getFresh($columns = [], $returnLazy = false)
// here to either the passed columns, or the standard default of retrieving
// all of the columns on the table using the "wildcard" column character.
if ($this->columns === null) {
$this->columns = (array) $columns;
$this->columns = $columns;
}

// Drop all columns if * is present, MongoDB does not work this way.
if (in_array('*', (array) $this->columns)) {
if (in_array('*', $this->columns)) {
$this->columns = [];
}

Expand Down Expand Up @@ -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))));

if ($function === null) {
if (! trait_exists(FluentFactoryTrait::class)) {
// This error will be unreachable when the mongodb/builder package will be merged into mongodb/mongodb
Expand Down

0 comments on commit b72017d

Please sign in to comment.