Persistence::Relationship::OneToMany - One to many relationship.


Persistence-Entity documentation Contained in the Persistence-Entity distribution.

Index


Code Index:

NAME

Top

Persistence::Relationship::OneToMany - One to many relationship.

CLASS HIERARCHY

Top

 Persistence::Relationship
    |
    +----Persistence::Relationship::OneToMany

SYNOPSIS

Top

    #.... entities definition
    my $membership_entity = Persistence::Entity->new(
        name    => 'wsus_user_service',
        alias   => 'us',
        primary_key => ['user_id', 'service_id'],
        columns => [
            sql_column(name => 'user_id'),
            sql_column(name => 'service_id'),
            sql_column(name => 'agreement_flag')
        ],
    );

    my $user_entity = Persistence::Entity->new(
        name    => 'wsus_user',
        alias   => 'ur',
        primary_key => ['id'],
        columns => [
            sql_column(name => 'id'),
            sql_column(name => 'username', unique => 1),
            sql_column(name => 'password'),
            sql_column(name => 'email'),
        ],
        to_many_relationships => [sql_relationship(target_entity => $membership_entity, join_columns => ['user_id'], order_by => 'service_id, user_id')]
    );
    $entity_manager->add_entities($membership_entity, $user_entity);

    # object mapping
    package User;

    use Abstract::Meta::Class ':all';
    use Persistence::Entity ':all';
    use Persistence::ORM ':all';

    entity 'wsus_user';
    column id => has('$.id');
    column username => has('$.name');
    column password => has('$.password');
    column email => has('$.email');

    one_to_many 'wsus_user_service' => (
        attribute    => has('@.membership' => (associated_class => 'Membership')),
        fetch_method => EAGER,
        cascade      => ALL,
    );

DESCRIPTION

Top

Represents one to many relationship. Allows cascading operation (inert/update/delete). Supports eager, lazy fetch, cascading operation (inert/update/delete).

EXPORT

Top

one_to_many method by ':all' tag.

METHODS

one_to_many

Create a new instance of one to many relation. Takes associated entity's id as parameters and list of named parameters for Persistence::Relationship::OneToMany constructor.

    one_to_many 'wsus_user_service' => (
        attribute    => has('@.membership' => (associated_class => 'Membership')),
        fetch_method => EAGER,
        cascade      => ALL,
    );




deserialise_attribute

Deserialises relation attribute

insert

Inserts relationship data.

merge

Merges relationship data.

delete

Merges relationship data.

SEE ALSO

Top

Persistence::Relationship Persistence::Entity Persistence::Entity::Manager Persistence::ORM

COPYRIGHT AND LICENSE

Top

AUTHOR

Top

Adrian Witas, adrian@webapp.strefa.pl


Persistence-Entity documentation Contained in the Persistence-Entity distribution.
package Persistence::Relationship::OneToMany;

use strict;
use warnings;

use vars qw(@EXPORT_OK %EXPORT_TAGS $VERSION);

use Abstract::Meta::Class ':all';
use base qw (Exporter Persistence::Relationship);
use Carp 'confess';

$VERSION = 0.01;

@EXPORT_OK = qw(one_to_many);
%EXPORT_TAGS = (all => \@EXPORT_OK);

sub one_to_many {
    my $package = caller();
    __PACKAGE__->add_relationship($package, @_);
}


sub deserialise_attribute {
    my ($self, $object, $entity_manager, $orm) = @_;
    my $entity = $entity_manager->entity($orm->entity_name);
    my $attribute = $self->attribute;
    my @rows = $entity->relationship_query(
        $self->name,
        ref($object) => $attribute->associated_class,
        $orm->column_values($object, $entity->primary_key)
    );

    if (@rows) {
        my $mutator = $attribute->mutator;
        $object->$mutator(\@rows);
    }
}


sub insert {
    my ($self, $orm, $entity, $unique_values, $object) = @_;
    my $values = $self->values($object);
    $entity->relationship_insert($self->name, $unique_values, @$values);
}


sub merge {
    my ($self, $orm, $entity, $unique_values, $object) = @_;
    my $values = $self->values($object);
    $entity->relationship_merge($self->name, $unique_values, @$values);
}



sub delete {
    my ($self, $orm, $entity, $unique_values, $object) = @_;
    my $values = $self->values($object);
    $entity->relationship_delete($self->name, $unique_values, @$values);
}

1;

__END__

1;