https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=41950 Bug ID: 41950 Summary: Make +count embeds sortable by using SQL-level COUNT subqueries Initiative type: --- Sponsorship --- status: Product: Koha Version: Main Hardware: All OS: All Status: NEW Severity: enhancement Priority: P5 - low Component: REST API Assignee: koha-bugs@lists.koha-community.org Reporter: tomascohen@gmail.com QA Contact: testopia@bugs.koha-community.org CC: tomascohen@gmail.com Currently, +count embeds (e.g., claims+count, checkouts+count) are computed in Perl via $object->$relation->count during to_api serialization. This means: 1. They cannot be used in _order_by — attempting to sort by a _count field (e.g., _order_by=-me.claims_count) causes a 500 error because DBIC cannot find the column in the database. 2. Each count triggers a separate SQL query per row (N+1 problem). This patch makes dbic_merge_prefetch detect +count embeds and, when a matching DBIC relationship exists, inject a correlated COUNT subquery via +select/+as instead of adding a prefetch. For example, claims+count on orders generates: sql (SELECT COUNT(*) FROM aqorders_claims WHERE aqorders_claims.ordernumber = me.ordernumber) AS claims_count The order_by entries referencing count aliases are fixed up to strip the me. prefix, since +as aliases are virtual columns not bound to a table. In to_api, the is_count handler now checks for the pre-computed column via get_column before falling back to the Perl-level ->count call. This preserves backward compatibility for Koha-level methods that don't have a corresponding DBIC relationship. -- You are receiving this mail because: You are the assignee for the bug. You are watching all bug changes.