From 893f45609b28a38a37e03b1d71a3227c1633ca7e Mon Sep 17 00:00:00 2001 From: henry701 Date: Fri, 7 Jun 2019 01:41:17 -0300 Subject: [PATCH 1/5] Handle case of array of ObjectIDs correctly In case an array of ObjectId-formatted Strings enter this method, this converts them to ObjectIds so that the query succeeds. Happens when calling 'get' method for one-to-many relations. --- src/Jenssegers/Mongodb/Query/Builder.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index 28d781a66..6fe25ce72 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -842,6 +842,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); } From 6a8c83de44062686ac83b73d1978e177ee406236 Mon Sep 17 00:00:00 2001 From: henry701 Date: Fri, 7 Jun 2019 01:44:10 -0300 Subject: [PATCH 2/5] Fix style --- src/Jenssegers/Mongodb/Query/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index 6fe25ce72..d480a5fd8 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -842,7 +842,7 @@ protected function performUpdate($query, array $options = []) */ public function convertKey($id) { - if(is_array($id)) { + if (is_array($id)) { return array_map(array($this, __FUNCTION__), $id); } From 1abb33037194bda00f9a33cac4a81a4834595dbd Mon Sep 17 00:00:00 2001 From: henry701 Date: Fri, 7 Jun 2019 02:23:10 -0300 Subject: [PATCH 3/5] Make BelongTo support one-to-many relationships By matching based on the value of the foreign key using 'In' in case it is an array, this achieves successful matching of one-to-many relationships. --- src/Jenssegers/Mongodb/Relations/BelongsTo.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Jenssegers/Mongodb/Relations/BelongsTo.php b/src/Jenssegers/Mongodb/Relations/BelongsTo.php index b47e856fa..4083e9924 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsTo.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsTo.php @@ -25,7 +25,15 @@ public function addConstraints() // 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}); + $foreign = $this->parent->{$this->foreignKey}; + 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); + } + else { + // It's a one-to-one relationship, just match using the classic equals operator. + $this->query->where($this->getOwnerKey(), '=', $foreign); + } } } From 954000ff3007c16a32e39bc72be0de8ece8bb2c3 Mon Sep 17 00:00:00 2001 From: henry701 Date: Thu, 23 Jan 2020 00:27:53 -0300 Subject: [PATCH 4/5] Add vscode folder to gitignores --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index c7e087c4c..74496a9e6 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ composer.lock *.project .idea/ .phpunit.result.cache +.vscode From 0efb70ea4ca741fc1db7325cd434db139276864d Mon Sep 17 00:00:00 2001 From: henry701 Date: Thu, 23 Jan 2020 00:42:37 -0300 Subject: [PATCH 5/5] Remove else (clean style) --- .../Mongodb/Relations/BelongsTo.php | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/Jenssegers/Mongodb/Relations/BelongsTo.php b/src/Jenssegers/Mongodb/Relations/BelongsTo.php index 4083e9924..3538ddfd6 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsTo.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsTo.php @@ -21,20 +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. - $foreign = $this->parent->{$this->foreignKey}; - 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); - } - else { - // It's a one-to-one relationship, just match using the classic equals operator. - $this->query->where($this->getOwnerKey(), '=', $foreign); - } + 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); } /**