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

Ordering in hasManyThrough relation doesn't work #292

Open
bbrody opened this issue Oct 24, 2019 · 3 comments
Open

Ordering in hasManyThrough relation doesn't work #292

bbrody opened this issue Oct 24, 2019 · 3 comments

Comments

@bbrody
Copy link

bbrody commented Oct 24, 2019

I have ProjectEntity with such relation in it
'users' => $mapper->hasManyThrough($entity, 'Api\V2\Entities\UserEntity', 'Api\V2\Entities\UserProjectEntity', 'user_id', 'project_id')->active()->order(['displayname' => 'ASC'])
->order() does not affect on the result, its sorted by id of UserProjectEntity (it has simple structure id - user_id -project_id)

@FlipEverything
Copy link
Contributor

Yeah, I don't think that will work out of the box. It's possibly a bug.

@bbrody
Copy link
Author

bbrody commented Nov 7, 2019

Hi! Any updates on this issue?

@Popoviciu-Andrei
Copy link

Popoviciu-Andrei commented Oct 3, 2023

You can fix this by editing the eagerLoadOnCollection method in vendor/vlucas/spot2/lib/Relation/HasManyThrough.php and changing this:
foreach ($collectionRelations as $relatedEntity) {
$relatedEntityPk = $relatedEntity->$relationRelatedForeignKey;

if ($relatedEntityPk == $throughForeignKey) {
    $entityRelations[$throughLocalKey][] = $relatedEntity;
}

}

to this:

foreach ($collectionRelations as $idx=>$relatedEntity) {
$relatedEntityPk = $relatedEntity->$relationRelatedForeignKey;

if ($relatedEntityPk == $throughForeignKey) {
    $entityRelations[$throughLocalKey][$idx] = $relatedEntity;
}

}
if(is_array($entityRelations[$throughLocalKey])) {
ksort($entityRelations[$throughLocalKey]);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants