Alzabo

 view release on metacpan or  search on metacpan

lib/Alzabo/ForeignKey.pm  view on Meta::CPAN

	     0..$#{ $self->{columns_from} } );
}

sub cardinality
{
    my $self = shift;

    return @{ $self->{cardinality} };
}

sub is_one_to_one
{
    my $self = shift;

    my @c = $self->cardinality;

    return $c[0] eq '1' && $c[1] eq '1';
}

sub is_one_to_many
{
    my $self = shift;

    my @c = $self->cardinality;

    return $c[0] eq '1' && $c[1] eq 'n';
}

sub is_many_to_one
{
    my $self = shift;

    my @c = $self->cardinality;

    return $c[0] eq 'n' && $c[1] eq '1';
}

sub from_is_dependent
{
    return shift->{from_is_dependent};
}

sub to_is_dependent
{
    return shift->{to_is_dependent};
}

sub is_same_relationship_as
{
    my ($self, $other) = @_;
    return ( $self->id eq $other->id
             or
             $self->id eq $other->reverse->id
           );
}

sub reverse
{
    my $self = shift;

    return bless { table_from        => $self->table_to,
                   table_to          => $self->table_from,
                   columns_from      => [ $self->columns_to ],
                   columns_to        => [ $self->columns_from ],
                   from_is_dependent => $self->to_is_dependent,
                   to_is_dependent   => $self->from_is_dependent,
                   cardinality       => [ reverse @{ $self->{cardinality} } ],
		 }, ref $self;
}

sub id
{
    my $self = shift;

    return join '___', ( ( map { $_->name }
			   $self->table_from,
			   $self->table_to,
			   $self->columns_from,
			   $self->columns_to,
			 ),
			 $self->cardinality,
			 $self->from_is_dependent,
			 $self->to_is_dependent,
		       );
}

sub comment { $_[0]->{comment} }

__END__

=head1 NAME

Alzabo::ForeignKey - Foreign key (relation) objects

=head1 SYNOPSIS

  use Alzabo::ForeignKey;

  foreach my $fk ($table->foreign_keys)
  {
      print $fk->cardinality;
  }

=head1 DESCRIPTION

A foreign key is an object defined by several properties.  It
represents a relationship from a column or columns in one table to a
column or columns in another table.

This relationship is defined by its cardinality (one to one, one to
many, or many to one) and its dependencies (whether or not table X is
dependent on table Y, and vice versa).

Many to many relationships are not allowed.  However, you may indicate
such a relationship when using the
L<Alzabo::Create::Schema-E<gt>add_relation
method|Alzabo::Create::Schema/add_relation> method, and it will create
the necessary intermediate linking table for you.

=head1 METHODS



( run in 2.717 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )