Skip to content

Commit 9ebcc5a

Browse files
authored
Merge pull request #1837 from stephandesouza/fix-hasmany
HasOne / HasMany must respect $localKey parameter
2 parents 20fd7b0 + 343e7bb commit 9ebcc5a

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

src/Jenssegers/Mongodb/Helpers/QueriesRelationships.php

+17-14
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
use Illuminate\Database\Eloquent\Relations\BelongsTo;
99
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
1010
use Illuminate\Database\Eloquent\Relations\HasOneOrMany;
11+
use Illuminate\Database\Eloquent\Relations\Relation;
1112
use Jenssegers\Mongodb\Eloquent\Model;
1213

1314
trait QueriesRelationships
1415
{
1516
/**
1617
* Add a relationship count / exists condition to the query.
17-
* @param string $relation
18+
* @param Relation|string $relation
1819
* @param string $operator
1920
* @param int $count
2021
* @param string $boolean
@@ -23,11 +24,13 @@ trait QueriesRelationships
2324
*/
2425
public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', Closure $callback = null)
2526
{
26-
if (strpos($relation, '.') !== false) {
27-
return $this->hasNested($relation, $operator, $count, $boolean, $callback);
28-
}
27+
if (is_string($relation)) {
28+
if (strpos($relation, '.') !== false) {
29+
return $this->hasNested($relation, $operator, $count, $boolean, $callback);
30+
}
2931

30-
$relation = $this->getRelationWithoutConstraints($relation);
32+
$relation = $this->getRelationWithoutConstraints($relation);
33+
}
3134

3235
// If this is a hybrid relation then we can not use a normal whereExists() query that relies on a subquery
3336
// We need to use a `whereIn` query
@@ -59,25 +62,25 @@ public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', C
5962
}
6063

6164
/**
62-
* @param $relation
65+
* @param Relation $relation
6366
* @return bool
6467
*/
65-
protected function isAcrossConnections($relation)
68+
protected function isAcrossConnections(Relation $relation)
6669
{
6770
return $relation->getParent()->getConnectionName() !== $relation->getRelated()->getConnectionName();
6871
}
6972

7073
/**
7174
* Compare across databases
72-
* @param $relation
75+
* @param Relation $relation
7376
* @param string $operator
7477
* @param int $count
7578
* @param string $boolean
7679
* @param Closure|null $callback
7780
* @return mixed
7881
* @throws Exception
7982
*/
80-
public function addHybridHas($relation, $operator = '>=', $count = 1, $boolean = 'and', Closure $callback = null)
83+
public function addHybridHas(Relation $relation, $operator = '>=', $count = 1, $boolean = 'and', Closure $callback = null)
8184
{
8285
$hasQuery = $relation->getQuery();
8386
if ($callback) {
@@ -99,10 +102,10 @@ public function addHybridHas($relation, $operator = '>=', $count = 1, $boolean =
99102
}
100103

101104
/**
102-
* @param $relation
105+
* @param Relation $relation
103106
* @return string
104107
*/
105-
protected function getHasCompareKey($relation)
108+
protected function getHasCompareKey(Relation $relation)
106109
{
107110
if (method_exists($relation, 'getHasCompareKey')) {
108111
return $relation->getHasCompareKey();
@@ -147,14 +150,14 @@ protected function getConstrainedRelatedIds($relations, $operator, $count)
147150

148151
/**
149152
* Returns key we are constraining this parent model's query with
150-
* @param $relation
153+
* @param Relation $relation
151154
* @return string
152155
* @throws Exception
153156
*/
154-
protected function getRelatedConstraintKey($relation)
157+
protected function getRelatedConstraintKey(Relation $relation)
155158
{
156159
if ($relation instanceof HasOneOrMany) {
157-
return $this->model->getKeyName();
160+
return $relation->getLocalKeyName();
158161
}
159162

160163
if ($relation instanceof BelongsTo) {

0 commit comments

Comments
 (0)