Skip to content

HasOne / HasMany must respect $localKey parameter #1837

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

Merged
merged 2 commits into from
Jan 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions src/Jenssegers/Mongodb/Helpers/QueriesRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasOneOrMany;
use Illuminate\Database\Eloquent\Relations\Relation;
use Jenssegers\Mongodb\Eloquent\Model;

trait QueriesRelationships
{
/**
* Add a relationship count / exists condition to the query.
* @param string $relation
* @param Relation|string $relation
* @param string $operator
* @param int $count
* @param string $boolean
Expand All @@ -23,11 +24,13 @@ trait QueriesRelationships
*/
public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', Closure $callback = null)
{
if (strpos($relation, '.') !== false) {
return $this->hasNested($relation, $operator, $count, $boolean, $callback);
}
if (is_string($relation)) {
if (strpos($relation, '.') !== false) {
return $this->hasNested($relation, $operator, $count, $boolean, $callback);
}

$relation = $this->getRelationWithoutConstraints($relation);
$relation = $this->getRelationWithoutConstraints($relation);
}

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

/**
* @param $relation
* @param Relation $relation
* @return bool
*/
protected function isAcrossConnections($relation)
protected function isAcrossConnections(Relation $relation)
{
return $relation->getParent()->getConnectionName() !== $relation->getRelated()->getConnectionName();
}

/**
* Compare across databases
* @param $relation
* @param Relation $relation
* @param string $operator
* @param int $count
* @param string $boolean
* @param Closure|null $callback
* @return mixed
* @throws Exception
*/
public function addHybridHas($relation, $operator = '>=', $count = 1, $boolean = 'and', Closure $callback = null)
public function addHybridHas(Relation $relation, $operator = '>=', $count = 1, $boolean = 'and', Closure $callback = null)
{
$hasQuery = $relation->getQuery();
if ($callback) {
Expand All @@ -99,10 +102,10 @@ public function addHybridHas($relation, $operator = '>=', $count = 1, $boolean =
}

/**
* @param $relation
* @param Relation $relation
* @return string
*/
protected function getHasCompareKey($relation)
protected function getHasCompareKey(Relation $relation)
{
if (method_exists($relation, 'getHasCompareKey')) {
return $relation->getHasCompareKey();
Expand Down Expand Up @@ -147,14 +150,14 @@ protected function getConstrainedRelatedIds($relations, $operator, $count)

/**
* Returns key we are constraining this parent model's query with
* @param $relation
* @param Relation $relation
* @return string
* @throws Exception
*/
protected function getRelatedConstraintKey($relation)
protected function getRelatedConstraintKey(Relation $relation)
{
if ($relation instanceof HasOneOrMany) {
return $this->model->getKeyName();
return $relation->getLocalKeyName();
}

if ($relation instanceof BelongsTo) {
Expand Down