Fey::Meta::HasMany::ViaFK - A parent for has-one metaclasses based on a L<Fey::FK> object


Fey-ORM documentation Contained in the Fey-ORM distribution.

Index


Code Index:

NAME

Top

Fey::Meta::HasMany::ViaFK - A parent for has-one metaclasses based on a Fey::FK object

VERSION

Top

version 0.43

DESCRIPTION

Top

This class implements a has-one relationship for a class, based on a provided (or deduced) Fey::FK object.

CONSTRUCTOR OPTIONS

Top

This class accepts the following constructor options:

* fk

If you don't provide this, the class looks for foreign keys between $self->table() and and $self->foreign_table(). If it finds exactly one, it uses that one.

* order_by

This will be appended to the SQL which is generated to select the foreign rows. It should be an arrayref which can be passed to Fey::SQL::Select->order_by().

* allows_undef

This defaults to true if any of the columns in the local table are NULLable, otherwise it defaults to false.

METHODS

Top

Besides the methods inherited from Fey::Meta::HasMany, it also provides the following methods:

$ho->fk()

Corresponds to the value passed to the constructor, or the calculated default.

$ho->order_by()

Corresponds to the value passed to the constructor.

AUTHOR

Top

Dave Rolsky <autarch@urth.org>

COPYRIGHT AND LICENSE

Top


Fey-ORM documentation Contained in the Fey-ORM distribution.

package Fey::Meta::HasMany::ViaFK;
BEGIN {
  $Fey::Meta::HasMany::ViaFK::VERSION = '0.43';
}

use strict;
use warnings;
use namespace::autoclean;

use Fey::ORM::Types qw( ArrayRef );
use List::AllUtils qw( any );

use Moose;
use MooseX::StrictConstructor;

with 'Fey::Meta::Role::Relationship::HasMany',
    'Fey::Meta::Role::Relationship::ViaFK';

has 'order_by' => (
    is  => 'ro',
    isa => ArrayRef,
);

sub _make_iterator_maker {
    my $self = shift;

    my $target_table = $self->foreign_table();

    my $select = $self->associated_class()->schema_class()->SQLFactoryClass()
        ->new_select();
    $select->select($target_table)->from($target_table);

    my @ph_names;

    my $ph = Fey::Placeholder->new();
    for my $pair ( @{ $self->fk()->column_pairs() } ) {
        my ( $from, $to ) = @{$pair};

        $select->where( $to, '=', $ph );

        push @ph_names, $from->name();
    }

    $select->order_by( @{ $self->order_by() } )
        if $self->order_by();

    my $bind_params_sub = sub {
        return map { $_[0]->$_() } @ph_names;
    };

    return $self->_make_subref_for_sql(
        $select,
        $bind_params_sub,
    );
}

__PACKAGE__->meta()->make_immutable();

1;

# ABSTRACT: A parent for has-one metaclasses based on a L<Fey::FK> object




__END__