spotorm / spot2

Spot v2.x DataMapper built on top of Doctrine's Database Abstraction Layer
http://phpdatamapper.com
BSD 3-Clause "New" or "Revised" License
601 stars 101 forks source link

Ordering in hasManyThrough relation doesn't work #292

Open bbrody opened 4 years ago

bbrody commented 4 years ago

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 commented 4 years ago

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

bbrody commented 4 years ago

Hi! Any updates on this issue?

Popoviciu-Andrei commented 9 months ago

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]); }