|
Lines 430-435
sub from_api_mapping {
Link Here
|
| 430 |
return $self->{_from_api_mapping}; |
430 |
return $self->{_from_api_mapping}; |
| 431 |
} |
431 |
} |
| 432 |
|
432 |
|
|
|
433 |
=head3 new_from_api |
| 434 |
|
| 435 |
my $object = Koha::Object->new_from_api; |
| 436 |
my $object = Koha::Object->new_from_api( $attrs ); |
| 437 |
|
| 438 |
Creates a new object, mapping the API attribute names to the ones on the DB schema. |
| 439 |
|
| 440 |
=cut |
| 441 |
|
| 442 |
sub new_from_api { |
| 443 |
my ( $class, $params ) = @_; |
| 444 |
|
| 445 |
my $self = $class->new; |
| 446 |
return $self->set_from_api( $params ); |
| 447 |
} |
| 448 |
|
| 449 |
=head3 set_from_api |
| 450 |
|
| 451 |
my $object = Koha::Object->new(...); |
| 452 |
$object->set_from_api( $attrs ) |
| 453 |
|
| 454 |
Sets the object's attributes mapping API attribute names to the ones on the DB schema. |
| 455 |
|
| 456 |
=cut |
| 457 |
|
| 458 |
sub set_from_api { |
| 459 |
my ( $self, $from_api_params ) = @_; |
| 460 |
|
| 461 |
return $self->set( $self->attributes_from_api( $from_api_params ) ); |
| 462 |
} |
| 463 |
|
| 433 |
=head3 $object->unblessed_all_relateds |
464 |
=head3 $object->unblessed_all_relateds |
| 434 |
|
465 |
|
| 435 |
my $everything_into_one_hashref = $object->unblessed_all_relateds |
466 |
my $everything_into_one_hashref = $object->unblessed_all_relateds |
|
Lines 551-577
Returns the passed params, converted from API naming into the model.
Link Here
|
| 551 |
=cut |
582 |
=cut |
| 552 |
|
583 |
|
| 553 |
sub attributes_from_api { |
584 |
sub attributes_from_api { |
| 554 |
my ( $self, $attributes ) = @_; |
585 |
my ( $self, $from_api_params ) = @_; |
| 555 |
|
586 |
|
| 556 |
my $mapping = $self->from_api_mapping; |
587 |
my $from_api_mapping = $self->from_api_mapping; |
| 557 |
|
588 |
|
| 558 |
foreach my $attribute ( keys %{$mapping} ) { |
589 |
my $params; |
| 559 |
my $mapped_attribute = $mapping->{$attribute}; |
590 |
|
| 560 |
if ( exists $attributes->{$attribute} |
591 |
while (my ($key, $value) = each %{ $from_api_params } ) { |
| 561 |
&& defined $mapped_attribute ) |
592 |
if ( exists $from_api_mapping->{$key} ) { |
| 562 |
{ |
593 |
$params->{$from_api_mapping->{$key}} = $value; |
| 563 |
# key => !undef |
|
|
| 564 |
$attributes->{$mapped_attribute} = delete $attributes->{$attribute}; |
| 565 |
} |
594 |
} |
| 566 |
elsif ( exists $attributes->{$attribute} |
595 |
else { |
| 567 |
&& !defined $mapped_attribute ) |
596 |
$params->{$key} = $value; |
| 568 |
{ |
|
|
| 569 |
# key => undef / to be deleted |
| 570 |
delete $attributes->{$attribute}; |
| 571 |
} |
597 |
} |
| 572 |
} |
598 |
} |
| 573 |
|
599 |
|
| 574 |
return $attributes; |
600 |
return $params; |
| 575 |
} |
601 |
} |
| 576 |
|
602 |
|
| 577 |
=head3 _type |
603 |
=head3 _type |
| 578 |
- |
|
|