Skip to content

Instantly share code, notes, and snippets.

@mrenvoize
Created July 3, 2018 11:04
Show Gist options
  • Save mrenvoize/3285acf8c87df08b8bd9dae34fa6607b to your computer and use it in GitHub Desktop.
Save mrenvoize/3285acf8c87df08b8bd9dae34fa6607b to your computer and use it in GitHub Desktop.
Chainable filters
=head3 filter_by_last_issued
Koha::Patrons->filter_by_last_issued( { after => DateTime, before => DateTime } );
Returns patrons filtered by whether their last issue falls betwen the passed limits.
=head4 arguments hashref
=over 4
=item before (optional) - filter out patrons whose last_issue was since DateTime
=item after (optional) - filter out patrons whose last_issue has been since DateTime
=back
=cut
sub filter_by_last_issued {
my ( $self, $options ) = @_;
return $self
unless ( defined($options)
&& ( $options->{before} || $options->{after} ) );
my $where = {};
my $attrs = {
join => 'old_issues',
'+select' => { max => 'old_issues.timestamp', -as => 'last_issued' },
'+as' => 'last_issued'
};
my $dtf = Koha::Database->new->schema->storage->datetime_parser;
push @{ $attrs->{'having'}->{'-and'} },
{ 'last_issued' => { '<' => $dtf->format_datetime( $options->{before} ) }
}
if $options->{before};
push @{ $attrs->{'having'}->{'-and'} },
{ 'last_issued' => { '>' => $dtf->format_datetime( $options->{after} ) } }
if $options->{after};
return $self->search( $where, $attrs );
}
@mrenvoize
Copy link
Author

My test script results in the below structure for attrs and where,

$attrs = {
          '+as' => 'last_issued',      
          '+select' => {                                                                                                                                
                         'max' => 'old_issues.timestamp',                                
                         '-as' => 'last_issued'                                  
                       },                                                  
          'having' => {                       
                        '-and' => [
                                    {             
                                      'last_issued' => {
                                                         '<' => '2018-06-28 11:02:47'            
                                                       }                
                                    }                                                       
                                  ]
                      },
          'join' => 'old_issues'
        };
$where = {};

but the final query fails with:

DBD::mysql::st execute failed: Unknown column 'last_issued' in 'having clause' [for Statement "SELECT COUNT( * ) FROM `borrowers` `me` LEFT JOIN `old_issues` `old_issues` ON `old_issues`.`borrowernumber` = `me`.`borrowernumber` WHERE ( ( `me`.`borrowernumber` = ? OR `me`.`borrowernumber` = ? OR `me`.`borrowernumber`
= ? ) ) HAVING `last_issued` < ?" with ParamValues: 0='1560', 1='1561', 2='1562', 3='2018-06-28 11:02:47'] at /usr/share/perl5/DBIx/Class/Storage/DBI.pm line 1836.
No method count found for Koha::Patrons DBIx::Class::Storage::DBI::_dbh_execute(): Unknown column 'last_issued' in 'having clause' at /home/martin/kohaclone/Koha/Objects.pm line 395

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment