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

Allow non-id primary key #5

Open
wants to merge 3 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
24 changes: 21 additions & 3 deletions src/ModeResolver/IdsModeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,33 @@ public function resolve($property, &$object, &$root, $fullPropertyPath)
// model, because the first item returned was a property
// We therefore just return the single ID
if (Utility::isPrimitive($firstElement)) {
return (int) Utility::getProperty($object, 'id');
$return = Utility::getProperty($object, $entry->getKeyName());

if ($entry->incrementing){
return (int) $return;
}

return $return;
}

return array_map(function ($entry) {
return (int) Utility::getProperty($entry, 'id');
$return = Utility::getProperty($entry, $entry->getKeyName());

if ($entry->incrementing){
return (int) $return;
}

return $return;
}, $object);
} elseif ($object instanceof Collection) {
return $object->map(function ($entry) {
return (int) Utility::getProperty($entry, 'id');
$return = Utility::getProperty($entry, $entry->getKeyName());

if ($entry->incrementing){
return (int) $return;
}

return $return;
});
// The relation is not a collection, but rather
// a singular relation
Expand Down
4 changes: 2 additions & 2 deletions src/ModeResolver/SideloadModeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ private function mergeRootCollection(&$collection, $object)
*/
private function addResourceToRootCollectionIfNonExistant(&$collection, $resource)
{
$identifier = Utility::getProperty($resource, 'id');
$identifier = Utility::getProperty($resource, $resource->getKeyName());
$exists = false;

$copy = $collection instanceof Collection ? $collection->toArray() : $collection;

foreach ($copy as $rootResource) {
if ((int) Utility::getProperty($rootResource, 'id') === (int) $identifier) {
if (Utility::getProperty($rootResource, $resource->getKeyName()) == $identifier) {
$exists = true;
break;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public static function getProperty($objectOrArray, $property)
if (is_array($objectOrArray)) {
return $objectOrArray[$property];
} else {
return $objectOrArray->{$property};
return $objectOrArray->getAttributes()[$property];
// return $objectOrArray->{$property};
}
}

Expand Down