Skip to content

merge #14

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 24 commits into from
Feb 8, 2020
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
ARG PHP_VERSION=7.2
ARG COMPOSER_VERSION=1.8

FROM composer:${COMPOSER_VERSION}
FROM composer:${COMPOSER_VERSION} as composer
FROM php:${PHP_VERSION}-cli

RUN apt-get update && \
apt-get install -y autoconf pkg-config libssl-dev git libzip-dev zlib1g-dev && \
apt-get install -y autoconf pkg-config libssl-dev git libzip-dev zlib1g-dev unzip --no-install-recommends && \
pecl install mongodb && docker-php-ext-enable mongodb && \
pecl install xdebug && docker-php-ext-enable xdebug && \
docker-php-ext-install -j$(nproc) pdo_mysql zip
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services:
context: .
dockerfile: Dockerfile
volumes:
- .:/code
- ./:/code
working_dir: /code
depends_on:
- mongodb
Expand Down
35 changes: 34 additions & 1 deletion src/Jenssegers/Mongodb/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Jenssegers\Mongodb\Relations;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model as EloquentModel;

class BelongsTo extends \Illuminate\Database\Eloquent\Relations\BelongsTo
Expand All @@ -25,7 +26,9 @@ 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});
$this->query
->where($this->getOwnerKey(), '=', $this->parent->{$this->foreignKey})
->orWhere($this->getOwnerKey().'._id', '=', $this->parent->{$this->foreignKey});
}
}

Expand Down Expand Up @@ -69,4 +72,34 @@ protected function whereInMethod(EloquentModel $model, $key)
{
return 'whereIn';
}

/**
* @inheritDoc
*/
public function match(array $models, Collection $results, $relation)
{
$foreign = $this->foreignKey;

$owner = $this->ownerKey;

// First we will get to build a dictionary of the child models by their primary
// key of the relationship, then we can easily match the children back onto
// the parents using that dictionary and the primary key of the children.
$dictionary = [];

foreach ($results as $result) {
$dictionary[$result->getAttribute($owner)] = $result;
}

// Once we have the dictionary constructed, we can loop through all the parents
// and match back onto their children using these keys of the dictionary and
// the primary key of the children to map them onto the correct instances.
foreach ($models as $model) {
if (isset($dictionary[(string) $model->{$foreign}])) {
$model->setRelation($relation, $dictionary[(string) $model->{$foreign}]);
}
}

return $models;
}
}
Loading