Skip to content

Commit ef1ae2d

Browse files
Optimizing int related id handling
- Fixes StyleCI
1 parent e362198 commit ef1ae2d

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

Diff for: src/Jenssegers/Mongodb/Helpers/QueriesRelationships.php

+16-8
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ protected function isAcrossConnections(Relation $relation)
7171
}
7272

7373
/**
74-
* Compare across databases
74+
* Compare across databases.
7575
* @param Relation $relation
7676
* @param string $operator
7777
* @param int $count
@@ -91,7 +91,7 @@ public function addHybridHas(Relation $relation, $operator = '>=', $count = 1, $
9191
$not = in_array($operator, ['<', '<=', '!=']);
9292
// If we are comparing to 0, we need an additional $not flip.
9393
if ($count == 0) {
94-
$not = !$not;
94+
$not = ! $not;
9595
}
9696

9797
$relations = $hasQuery->pluck($this->getHasCompareKey($relation));
@@ -123,9 +123,11 @@ protected function getHasCompareKey(Relation $relation)
123123
protected function getConstrainedRelatedIds($relations, $operator, $count)
124124
{
125125
$intHash = 'INT_RELATION_';
126-
$relationCount = array_count_values(array_map(function ($id) use ($intHash) {
127-
if (!is_string($id) && is_int($id)) {
128-
return $intHash . $id;
126+
$hasNumericIndex = false;
127+
$relationCount = array_count_values(array_map(function ($id) use ($intHash, &$hasNumericIndex) {
128+
if (! is_string($id) && is_int($id)) {
129+
$hasNumericIndex = true;
130+
return $intHash.$id;
129131
}
130132
return (string) $id; // Convert Back ObjectIds to Strings
131133
}, is_array($relations) ? $relations : $relations->flatten()->toArray()));
@@ -149,16 +151,22 @@ protected function getConstrainedRelatedIds($relations, $operator, $count)
149151
});
150152

151153
// All related ids.
154+
if (! $hasNumericIndex) {
155+
return array_keys($relationCount);
156+
}
157+
158+
// Has any numeric index?
152159
return array_map(static function ($id) use ($intHash) {
153160
if (strpos($id, $intHash, 0) === 0) {
154161
return (int) str_replace($intHash, '', $id);
155162
}
163+
156164
return (string) $id;
157165
}, array_keys($relationCount));
158166
}
159167

160168
/**
161-
* Returns key we are constraining this parent model's query with
169+
* Returns key we are constraining this parent model's query with.
162170
* @param Relation $relation
163171
* @return string
164172
* @throws Exception
@@ -173,10 +181,10 @@ protected function getRelatedConstraintKey(Relation $relation)
173181
return $relation->getForeignKeyName();
174182
}
175183

176-
if ($relation instanceof BelongsToMany && !$this->isAcrossConnections($relation)) {
184+
if ($relation instanceof BelongsToMany && ! $this->isAcrossConnections($relation)) {
177185
return $this->model->getKeyName();
178186
}
179187

180-
throw new Exception(class_basename($relation) . ' is not supported for hybrid query constraints.');
188+
throw new Exception(class_basename($relation).' is not supported for hybrid query constraints.');
181189
}
182190
}

Diff for: tests/RelationsTest.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
use Illuminate\Database\Eloquent\Collection;
@@ -438,10 +439,13 @@ public function testHasManyHas(): void
438439

439440
public function testHasNumeric(): void
440441
{
441-
$author1 = User::create(['_id' => '1', 'name' => 'George R. R. Martin']);
442-
$author1->books()->create(['_id' => '1', 'title' => 'A Game of Thrones', 'rating' => 5]);
442+
$author1 = User::create(['name' => 'George R. R. Martin']);
443+
$author1->books()->create(['title' => 'A Game of Thrones', 'rating' => 5]);
443444
$author1->books()->create(['_id' => '2', 'title' => 'A Clash of Kings', 'rating' => 5]);
444445

446+
$author2 = User::create(['name' => 'John Doe']);
447+
$author2->books()->create(['title' => 'My book', 'rating' => 2]);
448+
445449
$authors = User::has('books', '>', 1)->get();
446450
$this->assertCount(1, $authors);
447451
}

0 commit comments

Comments
 (0)