Fixed sorting for element choice type and added tests

This commit is contained in:
Jan Böhmer 2023-07-23 01:01:29 +02:00
parent 61f02d693f
commit 1ec4266f96
5 changed files with 167 additions and 5 deletions

View file

@ -116,8 +116,9 @@ class DBElementRepository extends EntityRepository
->getQuery();
$result = $q->getResult();
$result = array_combine($ids, $result);
$result = array_map(fn ($id) => $result[$id], $ids);
//Sort the result so that the elements are in the same order as the input array
$this->sortResultArrayByIDArray($result, $ids);
//Cache the result
$this->find_elements_by_id_cache[$cache_key] = $result;
@ -125,6 +126,20 @@ class DBElementRepository extends EntityRepository
return $result;
}
/**
* The elements in the result array will be sorted, so that their order of their IDs matches the order of the IDs in the input array.
* @param array $result_array
* @phpstan-param list<TEntityClass> $result_array
* @param int[] $ids
* @return void
*/
protected function sortResultArrayByIDArray(array &$result_array, array $ids): void
{
usort($result_array, static function (AbstractDBElement $a, AbstractDBElement $b) use ($ids) {
return array_search($a->getID(), $ids, true) <=> array_search($b->getID(), $ids, true);
});
}
protected function setField(AbstractDBElement $element, string $field, int $new_value): void
{
$reflection = new ReflectionClass($element::class);