X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FCDBICompat%2FHasA.pm;h=30109545341a4deaa85600bc1f4db92dda62df9a;hb=07037f89d4d9bf97c59a2c083de74f669521da47;hp=88e7caca3c8c454d9488824ff6f4de9374c4b8e3;hpb=c687b87e860c97257542dda2b14c0137f6fbc583;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/CDBICompat/HasA.pm b/lib/DBIx/Class/CDBICompat/HasA.pm index 88e7cac..3010954 100644 --- a/lib/DBIx/Class/CDBICompat/HasA.pm +++ b/lib/DBIx/Class/CDBICompat/HasA.pm @@ -4,84 +4,27 @@ use strict; use warnings; sub has_a { - my ($self, $col, $f_class) = @_; - die "No such column ${col}" unless $self->_columns->{$col}; + my ($self, $col, $f_class, %args) = @_; + $self->throw( "No such column ${col}" ) unless $self->_columns->{$col}; eval "require $f_class"; + if ($args{'inflate'} || $args{'deflate'}) { # Non-database has_a + if (!ref $args{'inflate'}) { + my $meth = $args{'inflate'}; + $args{'inflate'} = sub { $f_class->$meth(shift); }; + } + if (!ref $args{'deflate'}) { + my $meth = $args{'deflate'}; + $args{'deflate'} = sub { shift->$meth; }; + } + $self->inflate_column($col, \%args); + return 1; + } my ($pri, $too_many) = keys %{ $f_class->_primaries }; - die "has_a only works with a single primary key; ${f_class} has more" + $self->throw( "has_a only works with a single primary key; ${f_class} has more. try using a belongs_to relationship instead of Class::DBI compat rels" ) if $too_many; - $self->add_relationship($col, $f_class, - { "foreign.${pri}" => "self.${col}" }, - { _type => 'has_a' } ); - $self->mk_group_accessors('has_a' => $col); - return 1; -} -sub get_has_a { - my ($self, $rel) = @_; - #warn $rel; - #warn join(', ', %{$self->{_column_data}}); - return $self->{_relationship_data}{$rel} - if $self->{_relationship_data}{$rel}; - return undef unless $self->get_column($rel); - #my ($pri) = (keys %{$self->_relationships->{$rel}{class}->_primaries})[0]; - return $self->{_relationship_data}{$rel} = - ($self->search_related($rel, {}, {}))[0] - || do { - my $f_class = $self->_relationships->{$rel}{class}; - my ($pri) = keys %{$f_class->_primaries}; - $f_class->new({ $pri => $self->get_column($rel) }); }; -} - -sub set_has_a { - my ($self, $rel, @rest) = @_; - my $ret = $self->store_has_a($rel, @rest); - $self->{_dirty_columns}{$rel} = 1; - return $ret; -} - -sub store_has_a { - my ($self, $rel, $obj) = @_; - unless (ref $obj) { - delete $self->{_relationship_data}{$rel}; - return $self->store_column($rel, $obj); - } - my $rel_obj = $self->_relationships->{$rel}; - die "Can't set $rel: object $obj is not of class ".$rel_obj->{class} - unless $obj->isa($rel_obj->{class}); - $self->{_relationship_data}{$rel} = $obj; - #warn "Storing $obj: ".($obj->_ident_values)[0]; - $self->store_column($rel, ($obj->_ident_values)[0]); - return $obj; -} - -sub new { - my ($class, $attrs, @rest) = @_; - my %hasa; - foreach my $key (keys %$attrs) { - my $rt = $class->_relationships->{$key}{attrs}{_type}; - next unless $rt && $rt eq 'has_a' && ref $attrs->{$key}; - $hasa{$key} = delete $attrs->{$key}; - } - my $new = $class->NEXT::ACTUAL::new($attrs, @rest); - foreach my $key (keys %hasa) { - $new->store_has_a($key, $hasa{$key}); - } - return $new; -} - -sub _cond_value { - my ($self, $attrs, $key, $value) = @_; - if ( my $rel_obj = $self->_relationships->{$key} ) { - my $rel_type = $rel_obj->{attrs}{_type} || ''; - if ($rel_type eq 'has_a' && ref $value) { - die "Object $value is not of class ".$rel_obj->{class} - unless $value->isa($rel_obj->{class}); - $value = ($value->_ident_values)[0]; - #warn $value; - } - } - return $self->NEXT::ACTUAL::_cond_value($attrs, $key, $value); + $self->belongs_to($col, $f_class); + return 1; } 1;