|
Lines 123-128
sub build {
Link Here
|
| 123 |
# loop thru all fk and create linked records if needed |
123 |
# loop thru all fk and create linked records if needed |
| 124 |
# fills remaining entries in $col_values |
124 |
# fills remaining entries in $col_values |
| 125 |
my $foreign_keys = $self->_getForeignKeys( { source => $source } ); |
125 |
my $foreign_keys = $self->_getForeignKeys( { source => $source } ); |
|
|
126 |
|
| 126 |
for my $fk ( @$foreign_keys ) { |
127 |
for my $fk ( @$foreign_keys ) { |
| 127 |
# skip when FK points to itself: e.g. borrowers:guarantorid |
128 |
# skip when FK points to itself: e.g. borrowers:guarantorid |
| 128 |
next if $fk->{source} eq $source; |
129 |
next if $fk->{source} eq $source; |
|
Lines 337-358
sub _getForeignKeys {
Link Here
|
| 337 |
for my $rel_name( @relationships ) { |
338 |
for my $rel_name( @relationships ) { |
| 338 |
my $rel_info = $source->relationship_info($rel_name); |
339 |
my $rel_info = $source->relationship_info($rel_name); |
| 339 |
if( $rel_info->{attrs}->{is_foreign_key_constraint} ) { |
340 |
if( $rel_info->{attrs}->{is_foreign_key_constraint} ) { |
|
|
341 |
|
| 342 |
my $column_to_fk = {}; |
| 343 |
|
| 340 |
$rel_info->{source} =~ s/^.*:://g; |
344 |
$rel_info->{source} =~ s/^.*:://g; |
| 341 |
my $rel = { source => $rel_info->{source} }; |
345 |
my $rel = { source => $rel_info->{source} }; |
| 342 |
|
|
|
| 343 |
my @keys; |
346 |
my @keys; |
| 344 |
while( my ($col_fk_name, $col_name) = each(%{$rel_info->{cond}}) ) { |
347 |
# FIXME: This is not safe if we start adding |
|
|
348 |
# more complex conditions at DBIC level |
| 349 |
while ( my ( $col_fk_name, $col_name ) = each( %{ $rel_info->{cond} } ) ) { |
| 345 |
$col_name =~ s|self.(\w+)|$1|; |
350 |
$col_name =~ s|self.(\w+)|$1|; |
| 346 |
$col_fk_name =~ s|foreign.(\w+)|$1|; |
351 |
$col_fk_name =~ s|foreign.(\w+)|$1|; |
| 347 |
push @keys, { |
352 |
push @keys, |
| 348 |
col_name => $col_name, |
353 |
{ col_name => $col_name, |
| 349 |
col_fk_name => $col_fk_name, |
354 |
col_fk_name => $col_fk_name, |
| 350 |
}; |
355 |
}; |
|
|
356 |
$column_to_fk->{$col_name} = $col_fk_name; |
| 351 |
} |
357 |
} |
|
|
358 |
|
| 359 |
my $col_names = join( '-', sort keys %{ $column_to_fk } ); |
| 360 |
my $fk_names = join( '-', map { $column_to_fk->{$_} } sort keys %{ $column_to_fk } ); |
| 361 |
|
| 362 |
# In case more than one relationship has the same self.id and foreign.id, |
| 363 |
# but pointing to different sources (e.g. biblio vs deletedbiblio) |
| 364 |
my $default_rel = $self->_default_virtual_fk_relation( $params->{source}, $col_names, $fk_names ); |
| 365 |
# skip if there's a defined default relationship and it is not this one |
| 366 |
next if $default_rel and $default_rel ne $rel_name; |
| 367 |
|
| 352 |
# check if the combination table and keys is unique |
368 |
# check if the combination table and keys is unique |
| 353 |
# so skip double belongs_to relations (as in Biblioitem) |
369 |
# so skip double belongs_to relations (as in Biblioitem) |
| 354 |
my $tag = $rel->{source}. ':'. |
370 |
my $tag = $rel->{source} . ':' . $col_names; |
| 355 |
join ',', sort map { $_->{col_name} } @keys; |
|
|
| 356 |
next if $check_dupl->{$tag}; |
371 |
next if $check_dupl->{$tag}; |
| 357 |
$check_dupl->{$tag} = 1; |
372 |
$check_dupl->{$tag} = 1; |
| 358 |
$rel->{keys} = \@keys; |
373 |
$rel->{keys} = \@keys; |
|
Lines 695-700
returns the corresponding Koha::Object.
Link Here
|
| 695 |
|
710 |
|
| 696 |
my $patron = $builder->build_object({ class => 'Koha::Patrons' [, value => { ... }] }); |
711 |
my $patron = $builder->build_object({ class => 'Koha::Patrons' [, value => { ... }] }); |
| 697 |
|
712 |
|
|
|
713 |
=head1 Internal methods |
| 714 |
|
| 715 |
=head2 _default_virtual_fk_relation |
| 716 |
|
| 717 |
my $default_rel = $self->_default_virtual_fk_relation( $source, $col_names, $fk_names ); |
| 718 |
|
| 719 |
Internal method that maps the parameters to a preferred relationship name. |
| 720 |
|
| 721 |
Parameters |
| 722 |
|
| 723 |
=over 4 |
| 724 |
|
| 725 |
=item B<$source>: A DBIC schema class name (as returned by _type). |
| 726 |
|
| 727 |
=item B<$col_names>: The result of doing I<join( '-', @column_names )>. |
| 728 |
|
| 729 |
=item B<$fk_names>: The result of doing I<join( '-', @fk_names )> for the foreign keys that are mapped from the I<@column_names> in the exact same order. |
| 730 |
|
| 731 |
=back |
| 732 |
|
| 733 |
=cut |
| 734 |
|
| 735 |
sub _default_virtual_fk_relation { |
| 736 |
my ($self, $source, $col_names, $fk_names) = @_; |
| 737 |
|
| 738 |
my $default = { |
| 739 |
CheckoutRenewal => { |
| 740 |
checkout_id => { issue_id => 'checkout' }, |
| 741 |
}, |
| 742 |
}; |
| 743 |
|
| 744 |
return $default->{$source}->{$col_names}->{$fk_names}; |
| 745 |
} |
| 746 |
|
| 698 |
=head1 AUTHOR |
747 |
=head1 AUTHOR |
| 699 |
|
748 |
|
| 700 |
Yohann Dufour <yohann.dufour@biblibre.com> |
749 |
Yohann Dufour <yohann.dufour@biblibre.com> |
| 701 |
- |
|
|