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

[DRAFT] Support for Enum fields in relations #148

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
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 @@ -257,7 +258,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