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: [email protected]
          Reporter: [email protected]
        QA Contact: [email protected]
                CC: [email protected]

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.
_______________________________________________
Koha-bugs mailing list
[email protected]
https://lists.koha-community.org/cgi-bin/mailman/listinfo/koha-bugs
website : http://www.koha-community.org/
git : http://git.koha-community.org/
bugs : http://bugs.koha-community.org/

Reply via email to