Skip to content

Commit

Permalink
Added support for enum fields for relations in BelongsTo and HasOneOr…
Browse files Browse the repository at this point in the history
…Many.
  • Loading branch information
tommy-bolger committed Jan 14, 2023
1 parent a0d5fe5 commit 7b7585f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/Database/Eloquent/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Awobaz\Compoships\Database\Eloquent\Relations;

use BackedEnum;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
Expand Down Expand Up @@ -233,7 +234,12 @@ public function match(array $models, Collection $results, $relation)
foreach ($results as $result) {
if (is_array($owner)) { //Check for multi-columns relationship
$dictKeyValues = array_map(function ($k) use ($result) {
return $result->{$k};
// TODO: test
if ($result->{$k} instanceof BackedEnum) {
return $result->{$k}->value;
} else {
return $result->{$k};
}
}, $owner);

$dictionary[implode('-', $dictKeyValues)] = $result;
Expand All @@ -248,7 +254,12 @@ public function match(array $models, Collection $results, $relation)
foreach ($models as $model) {
if (is_array($foreign)) { //Check for multi-columns relationship
$dictKeyValues = array_map(function ($k) use ($model) {
return $model->{$k};
// TODO: test
if ($model->{$k} instanceof BackedEnum) {
return $model->{$k}->value;
} else {
return $model->{$k};
}
}, $foreign);

$key = implode('-', $dictKeyValues);
Expand Down
8 changes: 7 additions & 1 deletion src/Database/Eloquent/Relations/HasOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Awobaz\Compoships\Database\Eloquent\Relations;

use BackedEnum;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
Expand Down Expand Up @@ -231,7 +232,12 @@ protected function buildDictionary(Collection $results)
//If the foreign key is an array, we know it's a multi-column relationship...
if (is_array($foreign)) {
$dictKeyValues = array_map(function ($k) use ($result) {
return $result->{$k};
// TODO: test
if ($result->{$k} instanceof BackedEnum) {
return $result->{$k}->value;
} else {
return $result->{$k};
}
}, $foreign);
//... so we join the values to construct the dictionary key
$dictionary[implode('-', $dictKeyValues)][] = $result;
Expand Down

0 comments on commit 7b7585f

Please sign in to comment.