Bug 17196: Fix query builder for item search
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Thu, 25 Aug 2016 12:24:19 +0000 (13:24 +0100)
committerKyle M Hall <kyle@bywatersolutions.com>
Fri, 13 Jan 2017 13:49:26 +0000 (13:49 +0000)
The item search needs to join on biblio_metadata to allow search on
marcxml field

Test plan:
Launch complex item searches (using marc fields).

Signed-off-by: Mason James <mtj@kohaaloha.com>
Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
Signed-off-by: Zeno Tajoli <z.tajoli@cineca.it>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
C4/Items.pm

index e02da3b..b435fee 100644 (file)
@@ -112,6 +112,7 @@ This module contains an API for manipulating item
 records in Koha, and is used by cataloguing, circulation,
 acquisitions, and serials management.
 
+# FIXME This POD is not up-to-date
 A Koha item record is stored in two places: the
 items table and embedded in a MARC tag in the XML
 version of the associated bib record in C<biblioitems.marcxml>.
@@ -1304,7 +1305,6 @@ If this is set, it is set to C<One Order>.
 sub GetItemsInfo {
     my ( $biblionumber ) = @_;
     my $dbh   = C4::Context->dbh;
-    # note biblioitems.* must be avoided to prevent large marc and marcxml fields from killing performance.
     require C4::Languages;
     my $language = C4::Languages::getlanguage();
     my $query = "
@@ -2659,7 +2659,7 @@ sub _SearchItems_build_where_fragment {
                         $sqlfield = 'more_subfields_xml';
                         $xpath = '//record/datafield/subfield[@code="' . $marcsubfield . '"]';
                     } else {
-                        $sqlfield = 'marcxml';
+                        $sqlfield = 'metadata'; # From biblio_metadata
                         if ($marcfield < 10) {
                             $xpath = "//record/controlfield[\@tag=\"$marcfield\"]";
                         } else {
@@ -2768,11 +2768,16 @@ sub SearchItems {
         FROM items
           LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
           LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
+          LEFT JOIN biblio_metadata ON biblio_metadata.biblionumber = biblio.biblionumber
+          WHERE 1
     };
     if (defined $where_str and $where_str ne '') {
-        $query .= qq{ WHERE $where_str };
+        $query .= qq{ AND $where_str };
     }
 
+    $query .= q{ AND biblio_metadata.format = 'marcxml' AND biblio_metadata.marcflavour = ? };
+    push @where_args, C4::Context->preference('marcflavour');
+
     my @columns = Koha::Database->new()->schema()->resultset('Item')->result_source->columns;
     push @columns, Koha::Database->new()->schema()->resultset('Biblio')->result_source->columns;
     push @columns, Koha::Database->new()->schema()->resultset('Biblioitem')->result_source->columns;