<<

NAME

Koha::Object - Koha Object base class

SYNOPSIS

    use Koha::Object;
    my $object = Koha::Object->new({ property1 => $property1, property2 => $property2, etc... } );

DESCRIPTION

This class must always be subclassed.

API

Class Methods

Koha::Object->new();

my $object = Koha::Object->new(); my $object = Koha::Object->new($attributes);

Note that this cannot be used to retrieve record from the DB.

Koha::Object->_new_from_dbic();

my $object = Koha::Object->_new_from_dbic($dbic_row);

$object->store();

Saves the object in storage. If the object is new, it will be created. If the object previously existed, it will be updated.

Returns: $self if the store was a success undef if the store failed

discard_changes

Refetch the row from the DB

$object->update();

A shortcut for set + store in one call.

$object->delete();

Removes the object from storage.

Returns: The item object if deletion was a success The DBIX::Class error if deletion failed

$object->set( $properties_hashref )

$object->set( { property1 => $property1, property2 => $property2, property3 => $propery3, } );

Enables multiple properties to be set at once

Returns: 1 if all properties were set. 0 if one or more properties do not exist. undef if all properties exist but a different error prevents one or more properties from being set.

If one or more of the properties do not exist, no properties will be set.

$object->set_or_blank( $properties_hashref )

$object->set_or_blank( { property1 => $property1, property2 => $property2, property3 => $propery3, } );

If not listed in $properties_hashref, the property will be set to the default value defined at DB level, or nulled.

$object->unblessed();

Returns an unblessed representation of object.

$object->get_from_storage;

$object->object_messages

    my @messages = @{ $object->object_messages };

Returns the (probably non-fatal) messages that were recorded on the object.

$object->add_message

    try {
        <some action that might fail>
    }
    catch {
        if ( <fatal condition> ) {
            Koha::Exception->throw...
        }

        # This is a non fatal error, notify the caller
        $self->add_message({ message => $error, type => 'error' });
    }
    return $self;

Adds a message.

$object->TO_JSON

Returns an unblessed representation of the object, suitable for JSON output.

prefetch_whitelist

    my $whitelist = $object->prefetch_whitelist()

Returns a hash of prefetchable subs and the type they return.

to_api

    my $object_for_api = $object->to_api(
        {
          [ embed => {
                items => {
                    children => {
                        holds => {,
                            children => {
                              ...
                            }
                        }
                    }
                },
                library => {
                    ...
                }
            },
            public => 0|1,
            ...
         ]
        }
    );

Returns a representation of the object, suitable for API output.

to_api_mapping

    my $mapping = $object->to_api_mapping;

Generic method that returns the attribute name mappings required to render the object on the API.

Note: this only returns an empty hashref. Each class should have its own mapping returned.

strings_map

    my $string_map = $object->strings_map($params);

Generic method that returns the string map for coded attributes.

Return should be a hashref keyed on database field name with the values being hashrefs containing 'str', 'type' and optionally 'category'.

This is then used in to_api to render the _strings embed when requested.

Note: this only returns an empty hashref. Each class should have its own mapping returned.

public_read_list

    my @public_read_list = @{$object->public_read_list};

Generic method that returns the list of database columns that are allowed to be passed to render objects on the public API.

Note: this only returns an empty arrayref. Each class should have its own implementation.

unredact_list

    my @unredact_list = @{$object->unredact_list};

Generic method that returns the list of database columns that are allowed to be passed to render objects on the API when the user making the request should not ordinarily have unrestricted access to the data (as returned by the is_accesible method).

Note: this only returns an empty arrayref. Each class should have its own implementation.

from_api_mapping

    my $mapping = $object->from_api_mapping;

Generic method that returns the attribute name mappings so the data that comes from the API is correctly renamed to match what is required for the DB.

new_from_api

    my $object = Koha::Object->new_from_api;
    my $object = Koha::Object->new_from_api( $attrs );

Creates a new object, mapping the API attribute names to the ones on the DB schema.

set_from_api

    my $object = Koha::Object->new(...);
    $object->set_from_api( $attrs )

Sets the object's attributes mapping API attribute names to the ones on the DB schema.

attributes_from_api

    my $attributes = attributes_from_api( $params );

Returns the passed params, converted from API naming into the model.

$object->unblessed_all_relateds

my $everything_into_one_hashref = $object->unblessed_all_relateds

The unblessed method only retrieves column' values for the column of the object. In a *few* cases we want to retrieve the information of all the prefetched data.

$object->_result();

Returns the internal DBIC Row object

$object->_columns();

Returns an arrayref of the table columns

AUTOLOAD

The autoload method is used only to get and set values for an objects properties.

_type

This method must be defined in the child class. The value is the name of the DBIC resultset. For example, for borrowers, the _type method will return "Borrower".

_handle_to_api_child

is_accessible

    if ( $object->is_accessible ) { ... }

Stub method that is expected to be overloaded (if required) by implementing classes.

AUTHOR

Kyle M Hall <kyle@bywatersolutions.com>

Jonathan Druart <jonathan.druart@bugs.koha-community.org>

<<