Skip to content

Commit 5199e7c

Browse files
Merge branch 'feature/add-morph-to-relationship' into 2.x
2 parents 27a882d + db98ffb commit 5199e7c

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/Database/Eloquent/Concerns/FMHasRelationships.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use GearboxSolutions\EloquentFileMaker\Database\Eloquent\Relations\BelongsTo;
77
use GearboxSolutions\EloquentFileMaker\Database\Eloquent\Relations\HasMany;
88
use GearboxSolutions\EloquentFileMaker\Database\Eloquent\Relations\HasOne;
9+
use GearboxSolutions\EloquentFileMaker\Database\Eloquent\Relations\MorphTo;
910
use Illuminate\Database\Eloquent\Builder;
1011
use Illuminate\Database\Eloquent\Concerns\HasRelationships;
1112
use Illuminate\Database\Eloquent\Model;
@@ -133,6 +134,22 @@ public function morphMany($related, $name, $type = null, $id = null, $localKey =
133134
return $this->newMorphMany($instance->newQuery(), $this, $type, $id, $localKey);
134135
}
135136

137+
/**
138+
* Instantiate a new MorphTo relationship. Normally we would handle the resolution of the method here,
139+
* but we don't know yet if the related Modal is an FMModel or Eloquent model. We will let the FM MorphTo
140+
* class handle it.
141+
*
142+
* @param string $foreignKey
143+
* @param string $ownerKey
144+
* @param string $type
145+
* @param string $relation
146+
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
147+
*/
148+
protected function newMorphTo(Builder $query, Model $parent, $foreignKey, $ownerKey, $type, $relation)
149+
{
150+
return new MorphTo($query, $parent, $foreignKey, $ownerKey, $type, $relation);
151+
}
152+
136153
/**
137154
* Instantiate a new HasOne relationship.
138155
*
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace GearboxSolutions\EloquentFileMaker\Database\Eloquent\Relations;
4+
5+
use GearboxSolutions\EloquentFileMaker\Database\Eloquent\FMModel;
6+
use Illuminate\Database\Eloquent\Relations\MorphTo as BaseMorphTo;
7+
8+
class MorphTo extends BaseMorphTo
9+
{
10+
/**
11+
* Get all of the relation results for a type.
12+
*
13+
* @param string $type
14+
* @return \Illuminate\Database\Eloquent\Collection
15+
*/
16+
protected function getResultsByType($type)
17+
{
18+
$instance = $this->createModelByType($type);
19+
20+
if (! ($instance instanceof FMModel)) {
21+
return parent::getResultsByType($type);
22+
}
23+
24+
$ownerKey = $this->ownerKey ?? $instance->getKeyName();
25+
26+
$query = $this->replayMacros($instance->newQuery())
27+
->mergeConstraintsFrom($this->getQuery())
28+
->with(array_merge(
29+
$this->getQuery()->getEagerLoads(),
30+
(array) ($this->morphableEagerLoads[get_class($instance)] ?? [])
31+
))
32+
->withCount(
33+
(array) ($this->morphableEagerLoadCounts[get_class($instance)] ?? [])
34+
);
35+
36+
if ($callback = ($this->morphableConstraints[get_class($instance)] ?? null)) {
37+
$callback($query);
38+
}
39+
40+
$whereIn = $this->whereInMethod($instance, $ownerKey);
41+
42+
return $query->{$whereIn}(
43+
$ownerKey, $this->gatherKeysByType($type, $instance->getKeyType())
44+
)->get();
45+
}
46+
}

0 commit comments

Comments
 (0)