@@ -, +, @@ for unique_id - Apply the unit tests patches - Run: $ prove t/db_dependent/Koha/Patron/Attributes.t - Apply this patch - Run: $ prove t/db_dependent/Koha/Patron/Attributes.t - Sign off :-D --- Koha/Exceptions/Patron/Attribute.pm | 16 ++++++++++++++++ Koha/Patron/Attribute.pm | 28 ++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 Koha/Exceptions/Patron/Attribute.pm --- a/Koha/Exceptions/Patron/Attribute.pm +++ a/Koha/Exceptions/Patron/Attribute.pm @@ -0,0 +1,16 @@ +package Koha::Exceptions::Patron::Attribute; + +use Modern::Perl; + +use Exception::Class ( + + 'Koha::Exceptions::Patron::Attribute' => { + description => 'Something went wrong' + }, + 'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint' => { + isa => 'Koha::Exceptions::Patron::Attribute', + description => "unique_id set for attribute type and tried to add a new attribute for the same code" + } +); + +1; --- a/Koha/Patron/Attribute.pm +++ a/Koha/Patron/Attribute.pm @@ -17,6 +17,8 @@ package Koha::Patron::Attribute; use Modern::Perl; +use Koha::Database; +use Koha::Exceptions::Patron::Attribute; use Koha::Patron::Attribute::Types; use base qw(Koha::Object); @@ -31,6 +33,32 @@ Koha::Patron::Attribute - Koha Patron Attribute Object class =cut +=head3 store + + my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... }); + try { $attribute->store } + catch { handle_exception }; + +=cut + +sub store { + + my $self = shift; + + if ( Koha::Patron::Attribute::Types->find( $self->code )->unique_id ) { + my $attr_count + = Koha::Database->new->schema->resultset( $self->_type )->search( + { borrowernumber => $self->borrowernumber, + code => $self->code + } + )->count; + Koha::Exceptions::Patron::Attribute::UniqueIDConstraint->throw() + if $attr_count > 0; + } + + return $self->SUPER::store(); +} + =head3 opac_display my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... }); --