Skip to content
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

Set empty relations as loaded too and prevent unnecessary database queries. #116

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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 composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "elasticquent/elasticquent",
"name": "mendicm/elasticquent",
"type": "library",
"description": "Maps Laravel Eloquent models to Elasticsearch types.",
"keywords": [
Expand All @@ -20,7 +20,7 @@
"php": ">=5.4.0",
"illuminate/database": "~4.2|^5",
"illuminate/config": "~4.2|^5",
"nesbot/carbon": "~1.0",
"nesbot/carbon": "~1.0|^2",
"elasticsearch/elasticsearch": "~6.0"
},
"require-dev": {
Expand Down
11 changes: 9 additions & 2 deletions src/ElasticquentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use ReflectionMethod;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\Relation;

/**
Expand Down Expand Up @@ -706,7 +707,7 @@ public static function loadRelationsAttributesRecursive(Model $model)
$reflection_method = new ReflectionMethod($model, $key);

// Check if method class has or inherits Illuminate\Database\Eloquent\Model
if(!static::isClassInClass("Illuminate\Database\Eloquent\Model", $reflection_method->class)) {
if(static::isClassInClass("Illuminate\Database\Eloquent\Model", $reflection_method->class)) {
$relation = $model->$key();

if ($relation instanceof Relation) {
Expand All @@ -720,6 +721,12 @@ public static function loadRelationsAttributesRecursive(Model $model)
// Unset attribute before match relation
unset($model[$key]);
$relation->match([$model], $models, $key);

// The match method doesn't set the relation as loaded if the $models collection is empty
// Set the relation as loaded manually and avoid future database queries
if ((count($value) == 1 && reset($value) === null) || empty($value)) {
$model->setRelation($key, empty($value) ? new Collection() : null);
}
}
}
}
Expand All @@ -737,7 +744,7 @@ public static function loadPivotAttribute(Model $model, Relation $parentRelation
$attributes = $model->getAttributes();

foreach ($attributes as $key => $value) {
if ($key === 'pivot') {
if ($key === 'pivot' && $parentRelation) {
unset($model[$key]);
$pivot = $parentRelation->newExistingPivot($value);
$model->setRelation($key, $pivot);
Expand Down