Skip to content

Commit

Permalink
Better order by for linked resources table; consistent ordering betwe…
Browse files Browse the repository at this point in the history
…en table and select
  • Loading branch information
jimsafley committed Apr 4, 2024
1 parent c96c0d0 commit e4c918d
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions application/src/Api/Adapter/AbstractResourceEntityAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,9 @@ public function getSubjectValues(Resource $resource, $page = null, $perPage = nu
'property.label property_label',
'resource_template_property.id resource_template_property_id',
'resource_template_property.alternateLabel property_alternate_label',
"CASE WHEN resource_template_property.alternateLabel IS NOT NULL AND resource_template_property.alternateLabel NOT LIKE '' THEN resource_template_property.alternateLabel ELSE property.label END order_by_label",
])
->orderBy('resource_template_property.alternateLabel, property.label, property.id, resource.title')
->orderBy('property.id, order_by_label, resource.title')
->setMaxResults($perPage)
->setFirstResult($offset);
$event = new Event('api.subject_values.query', $this, [
Expand Down Expand Up @@ -661,14 +662,16 @@ public function getSubjectValueProperties(Resource $resource, $resourceType = nu
->join('value.property', 'property')
->join('property.vocabulary', 'vocabulary')
->select([
"CONCAT(vocabulary.prefix, ':', property.localName) term",
'property.id property_id',
'resource_template_property.id resource_template_property_id',
'property.label property_label',
'resource_template_property.alternateLabel property_alternate_label',
"CONCAT(vocabulary.prefix, ':', property.localName) term",
])
->orderBy('property.id, resource_template_property.id');
// Group the properties by label.
// Group the properties by property ID then label. We must use code to
// group instead of a SQL "GROUP BY" because of the special case where
// there is no resource template property.
$results = [];
foreach ($qb->getQuery()->getResult() as $result) {
if ($result['property_alternate_label']) {
Expand All @@ -690,11 +693,14 @@ public function getSubjectValueProperties(Resource $resource, $resourceType = nu
foreach ($properties as $label => $data) {
$subjectValueProperties[] = [
'label' => $label,
'property_id' => $propertyId,
'term' => $data['term'],
'compound_id' => sprintf('%s:%s-%s', $resourceType, $propertyId, implode(',', array_unique($data['resource_template_property_ids']))),
];
}
}
// Sort the properties by property ID then label.
usort($subjectValueProperties, fn($a, $b) => strcmp($a['property_id'] . $a['label'], $b['property_id'] . $b['label']));
return $subjectValueProperties;
}

Expand Down

0 comments on commit e4c918d

Please sign in to comment.