View | Details | Raw Unified | Return to bug 8309
Collapse All | Expand All

(-)a/Koha/BusinessLogic/Borrower.pm (+5 lines)
Line 0 Link Here
1
package Koha::BusinessLogic::Borrower;
2
3
use Modern::Perl;
4
use Moose;
5
extends (qw(Koha::DataObject::Borrower));
(-)a/Koha/DB/Borrower.pm (+43 lines)
Line 0 Link Here
1
package Koha::DB::Borrower;
2
3
use Modern::Perl;
4
use C4::Context; #FIXME = create a Koha package for KOHA_CONF reading
5
6
use Koha::Schema;
7
8
use Moose;
9
10
has 'borrowernumber' => (is => 'rw', required => 1, isa => 'Str');
11
has 'surname' => (is => 'rw', required => 0, isa => 'Str');
12
has 'firstname' => (is => 'rw', required => 0, isa => 'Str');
13
has 'branchcode' => (is => 'rw', required => 0, isa => 'Str');
14
15
16
my $schema = Koha::Schema->connect(
17
    'dbi:mysql:dbname='.C4::Context->config("database"),
18
    C4::Context->config("user"),
19
    C4::Context->config("pass"),
20
    { AutoCommit => 1 },
21
  );
22
23
sub create {
24
    my ($self) = @_;
25
    $schema->resultset('Borrower')->create({ map { $_ => $self->$_ } $schema->source('Borrower')->columns });
26
}
27
28
sub read {
29
    my ($self, $borrower) = @_;
30
    # load parameter tables attached to the borrower (branches and categories)
31
    return $schema->resultset('Borrower')->search($borrower, { prefetch => ['branchcode','categorycode'], { cache_for => 2 }});
32
}
33
34
sub update {
35
    my ($self, $borrower) = @_;
36
    return $schema->resultset('Borrower')->search({borrowernumber => $borrower->{borrowernumber}})->update($borrower);
37
}
38
39
sub delete {
40
    my ($self,$borrower) = @_;
41
    $schema->resultset('Borrower')->search($borrower)->delete;
42
}
43
(-)a/Koha/DataObject/Borrower.pm (+5 lines)
Line 0 Link Here
1
package Koha::DataObject::Borrower;
2
3
use Modern::Perl;
4
use Moose;
5
extends (qw(Koha::DB::Borrower));
(-)a/testrelations.pl (-1 / +80 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
use v5.10.0;
4
use Modern::Perl;
5
#use Koha::BusinessLogic::Branch2;
6
#use Koha::BusinessLogic::Branchrelation;
7
use Koha::BusinessLogic::Borrower;
8
9
# how many SQL queries did mysql recieved before this script is executed
10
my (undef, $before) = split ' ',`mysql -e "show status"|grep Queries`;
11
12
my$borrowernumber='7695';
13
14
# retrieve a patron
15
my $borrower=Koha::BusinessLogic::Borrower->read({'borrowernumber' => $borrowernumber})->first;
16
# show his name
17
say $borrower->firstname;
18
say $borrower->surname;
19
# show the branchname, very easy !!!
20
say $borrower->branchcode->branchname;
21
# same thing with category
22
say $borrower->categorycode->description;
23
# now retrieve all items issued to this patron
24
# note that each line result in 3 SQL queries being done
25
# possible improvement => prefetch the link between  
26
foreach my $checkout ($borrower->issues->all) {
27
    # show the itemnumber
28
    say "Item currently checked-out:".$checkout->itemnumber->itemnumber;
29
    # show book title
30
    say "Item currently checked-out:".$checkout->itemnumber->biblioitemnumber->biblionumber->title;
31
}
32
foreach my $checkout ($borrower->old_issues->all) {
33
    # show the itemnumber
34
    say "Item previously checked-out:".$checkout->itemnumber->itemnumber;
35
    # show book title
36
    say "Item previously checked-out:".$checkout->itemnumber->biblioitemnumber->biblionumber->title;
37
}
38
39
#use Data::Dumper;
40
# get 1st categorycode
41
#say "Fetching 1st categorycode";
42
#my $branch=Koha::BusinessLogic::Branch2->read({'branchcode' => 'MAURES'})->first;
43
#say $branch->branchname;
44
#my $branchrelation = $branch->branchrelations->first;
45
#say "CODE".$branchrelation->categorycode->categorycode;
46
# this also add a SQL query, why ?
47
#say "BRANCH".$branchrelation->branchcode->branchcode; 
48
49
#
50
#say Koha::BusinessLogic::Branch2->read({'branchcode' => 'TEST'})->first->branchrelations->first->categorycode->categorycode;
51
# the following line add 3 queries => no caching at all
52
#say Koha::BusinessLogic::Branch2->read({'branchcode' => 'TEST'})->first->branchrelations->first->branchcode->branchcode;
53
54
# get all categorycodes
55
#say "Fetching all categorycodes";
56
#foreach (Koha::BusinessLogic::Branch2->read({'branchcode' => 'TEST'})->first->branchrelations->all) {
57
#    say $_->categorycode->categorycode;
58
#}
59
60
# how many SQL queries did mysql recieve after this script is executed
61
my (undef, $after) = split ' ',`mysql -e "show status"|grep Queries`;
62
# the difference between before and after tell us how many queries have been made
63
say "Total queries: ".($after-$before-3); # -3 is the number of queries "show status" itself requires
64
65
# testing column = it does not require any SQL query, ++ !
66
#my $schema = Koha::Schema->connect(
67
#    'dbi:mysql:dbname='.C4::Context->config("database"),
68
#    C4::Context->config("user"),
69
#    C4::Context->config("pass"),
70
#    { AutoCommit => 1,
71
#      #cursor_class => 'DBIx::Class::Cursor::Cached',
72
#    },
73
#  );
74
#(undef, $before) = split ' ',`mysql -e "show status"|grep Queries`;
75
#my @temp = $schema->source('Branch')->columns;
76
#say @temp;
77
#(undef, $after) = split ' ',`mysql -e "show status"|grep Queries`;
78
## the difference between before and after tell us how many queries have been made
79
#say "Total queries for columns: ".($after-$before-3); # -3 is the number of queries "show status" itself requires
80
# 

Return to bug 8309