DBIx::Class::CDBICompat::Constructor
DBIx::Class::CDBICompat::AccessorMapping
DBIx::Class::CDBICompat::ColumnCase
+ DBIx::Class::CDBICompat::MightHave
DBIx::Class::CDBICompat::HasMany
DBIx::Class::CDBICompat::HasA
DBIx::Class::CDBICompat::LazyLoading
$class->throw( "No such column ${f_key} on foreign class ${f_class}" )
unless $f_class->_columns->{$f_key};
$args ||= {};
- my $cascade = not (ref $args eq 'HAS' && delete $args->{no_cascade_delete});
+ my $cascade = not (ref $args eq 'HASH' && delete $args->{no_cascade_delete});
$class->add_relationship($rel, $f_class,
{ "foreign.${f_key}" => "self.${self_key}" },
{ accessor => 'multi',
--- /dev/null
+package DBIx::Class::CDBICompat::MightHave;
+
+use strict;
+use warnings;
+
+sub might_have {
+ my ($class, $rel, $f_class, @columns) = @_;
+ my ($pri, $too_many) = keys %{ $class->_primaries };
+ $class->throw( "might_have only works with a single primary key; ${class} has more" )
+ if $too_many;
+ my $f_pri;
+ ($f_pri, $too_many) = keys %{ $f_class->_primaries };
+ $class->throw( "might_have only works with a single primary key; ${f_class} has more" )
+ if $too_many;
+ $class->add_relationship($rel, $f_class,
+ { "foreign.${f_pri}" => "self.${pri}" },
+ { accessor => 'single', proxy => \@columns,
+ cascade_update => 1, cascade_delete => 1 });
+ 1;
+}
+
+1;
use base qw/DBIx::Class::Relationship::Accessor
DBIx::Class::Relationship::CascadeActions
+ DBIx::Class::Relationship::ProxyMethods
DBIx::Class::Relationship
DBIx::Class::InflateColumn
DBIx::Class::SQL::OrderBy
sub new_related {
my ($self, $rel, $values, $attrs) = @_;
- $self->throw( "Can't call create_related as class method" )
+ $self->throw( "Can't call new_related as class method" )
unless ref $self;
- $self->throw( "create_related needs a hash" )
+ $self->throw( "new_related needs a hash" )
unless (ref $values eq 'HASH');
my $rel_obj = $self->_relationships->{$rel};
$self->throw( "No such relationship ${rel}" ) unless $rel_obj;
} elsif (exists $self->{_relationship_data}{$rel}) {
return $self->{_relationship_data}{$rel};
} else {
- return $self->{_relationship_data}{$rel}
- = $self->find_or_create_related($rel, {}, {});
+ my ($val) = $self->search_related($rel, {}, {});
+ return unless $val;
+ return $self->{_relationship_data}{$rel} = $val;
}
};
} elsif ($acc_type eq 'filter') {
--- /dev/null
+package DBIx::Class::Relationship::ProxyMethods;
+
+use strict;
+use warnings;
+
+use base qw/Class::Data::Inheritable/;
+
+sub add_relationship {
+ my ($class, $rel, @rest) = @_;
+ my $ret = $class->NEXT::ACTUAL::add_relationship($rel => @rest);
+ if (my $proxy_list = $class->_relationships->{$rel}->{attrs}{proxy}) {
+ no strict 'refs';
+ no warnings 'redefine';
+ foreach my $proxy (ref $proxy_list ? @$proxy_list : $proxy_list) {
+ *{"${class}::${proxy}"} =
+ sub {
+ my $self = shift;
+ my $val = $self->$rel;
+ if (@_ && !defined $val) {
+ $val = $self->create_related($rel, { $proxy => $_[0] });
+ @_ = ();
+ }
+ return ($val ? $val->$proxy(@_) : undef);
+ }
+ }
+ }
+ return $ret;
+}
+
+1;
return defined($exists) ? $exists : $class->create($hash);
}
+sub insert_or_update {
+ my $self = shift;
+ return ($self->in_database ? $self->update : $self->insert);
+}
+
sub retrieve_all {
my ($class) = @_;
return $class->retrieve_from_sql( '1' );