Skip to content

Commit b3a500a

Browse files
authored
Merge pull request #14 from divine/pr/13
merge
2 parents 4d81b40 + 964266a commit b3a500a

File tree

9 files changed

+361
-93
lines changed

9 files changed

+361
-93
lines changed

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
ARG PHP_VERSION=7.2
22
ARG COMPOSER_VERSION=1.8
33

4-
FROM composer:${COMPOSER_VERSION}
4+
FROM composer:${COMPOSER_VERSION} as composer
55
FROM php:${PHP_VERSION}-cli
66

77
RUN apt-get update && \
8-
apt-get install -y autoconf pkg-config libssl-dev git libzip-dev zlib1g-dev && \
8+
apt-get install -y autoconf pkg-config libssl-dev git libzip-dev zlib1g-dev unzip --no-install-recommends && \
99
pecl install mongodb && docker-php-ext-enable mongodb && \
1010
pecl install xdebug && docker-php-ext-enable xdebug && \
1111
docker-php-ext-install -j$(nproc) pdo_mysql zip

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ services:
77
context: .
88
dockerfile: Dockerfile
99
volumes:
10-
- .:/code
10+
- ./:/code
1111
working_dir: /code
1212
depends_on:
1313
- mongodb

src/Jenssegers/Mongodb/Relations/BelongsTo.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Jenssegers\Mongodb\Relations;
44

55
use Illuminate\Database\Eloquent\Builder;
6+
use Illuminate\Database\Eloquent\Collection;
67
use Illuminate\Database\Eloquent\Model as EloquentModel;
78

89
class BelongsTo extends \Illuminate\Database\Eloquent\Relations\BelongsTo
@@ -25,7 +26,9 @@ public function addConstraints()
2526
// For belongs to relationships, which are essentially the inverse of has one
2627
// or has many relationships, we need to actually query on the primary key
2728
// of the related models matching on the foreign key that's on a parent.
28-
$this->query->where($this->getOwnerKey(), '=', $this->parent->{$this->foreignKey});
29+
$this->query
30+
->where($this->getOwnerKey(), '=', $this->parent->{$this->foreignKey})
31+
->orWhere($this->getOwnerKey().'._id', '=', $this->parent->{$this->foreignKey});
2932
}
3033
}
3134

@@ -69,4 +72,34 @@ protected function whereInMethod(EloquentModel $model, $key)
6972
{
7073
return 'whereIn';
7174
}
75+
76+
/**
77+
* @inheritDoc
78+
*/
79+
public function match(array $models, Collection $results, $relation)
80+
{
81+
$foreign = $this->foreignKey;
82+
83+
$owner = $this->ownerKey;
84+
85+
// First we will get to build a dictionary of the child models by their primary
86+
// key of the relationship, then we can easily match the children back onto
87+
// the parents using that dictionary and the primary key of the children.
88+
$dictionary = [];
89+
90+
foreach ($results as $result) {
91+
$dictionary[$result->getAttribute($owner)] = $result;
92+
}
93+
94+
// Once we have the dictionary constructed, we can loop through all the parents
95+
// and match back onto their children using these keys of the dictionary and
96+
// the primary key of the children to map them onto the correct instances.
97+
foreach ($models as $model) {
98+
if (isset($dictionary[(string) $model->{$foreign}])) {
99+
$model->setRelation($relation, $dictionary[(string) $model->{$foreign}]);
100+
}
101+
}
102+
103+
return $models;
104+
}
72105
}

0 commit comments

Comments
 (0)