Skip to content

Commit

Permalink
Merge 7605464 into c55d42d
Browse files Browse the repository at this point in the history
  • Loading branch information
henry701 committed Feb 3, 2020
2 parents c55d42d + 7605464 commit fd1e50a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -8,3 +8,4 @@ composer.lock
*.project
.idea/
.phpunit.result.cache
.vscode
4 changes: 4 additions & 0 deletions src/Jenssegers/Mongodb/Query/Builder.php
Expand Up @@ -844,6 +844,10 @@ protected function performUpdate($query, array $options = [])
*/
public function convertKey($id)
{
if (is_array($id)) {
return array_map(array($this, __FUNCTION__), $id);
}

if (is_string($id) && strlen($id) === 24 && ctype_xdigit($id)) {
return new ObjectID($id);
}
Expand Down
19 changes: 14 additions & 5 deletions src/Jenssegers/Mongodb/Relations/BelongsTo.php
Expand Up @@ -21,12 +21,21 @@ public function getHasCompareKey()
*/
public function addConstraints()
{
if (static::$constraints) {
// For belongs to relationships, which are essentially the inverse of has one
// or has many relationships, we need to actually query on the primary key
// of the related models matching on the foreign key that's on a parent.
$this->query->where($this->getOwnerKey(), '=', $this->parent->{$this->foreignKey});
if (!static::$constraints) {
return;
}
// For belongs to relationships, which are essentially the inverse of has one
// or has many relationships, we need to actually query on the primary key
// of the related models matching on the foreign key that's on a parent.
$foreign = $this->parent->{$this->foreignKey};
// Check if the foreign key is an array, so we can infer the cardinality of the relationship.
if (is_array($foreign)) {
// This means that it is a one-to-many relationship. Try a match using whereIn.
$this->query->whereIn($this->getOwnerKey(), $foreign);
return;
}
// It's a one-to-one relationship, just match using the classic equals operator.
$this->query->where($this->getOwnerKey(), '=', $foreign);
}

/**
Expand Down

0 comments on commit fd1e50a

Please sign in to comment.