X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FCDBICompat%2FRelationships.pm;h=abd67830d6b78a807f71695b0c01a793308fdaef;hb=5e0eea3522876a30453af24097507198bbbc9409;hp=f4109768e3239591ed9462ec7b8262efb8f347ec;hpb=4656f62f9425820ef15c30e2cc6bfb0bff2db423;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/CDBICompat/Relationships.pm b/lib/DBIx/Class/CDBICompat/Relationships.pm index f410976..abd6783 100644 --- a/lib/DBIx/Class/CDBICompat/Relationships.pm +++ b/lib/DBIx/Class/CDBICompat/Relationships.pm @@ -3,18 +3,19 @@ package # hide from PAUSE use strict; use warnings; - -use base qw/Class::Data::Inheritable/; +use base 'DBIx::Class'; use Clone; use DBIx::Class::CDBICompat::Relationship; +use Scalar::Util 'blessed'; +use DBIx::Class::_Util qw(quote_sub perlstring); __PACKAGE__->mk_classdata('__meta_info' => {}); =head1 NAME -DBIx::Class::CDBICompat::Relationships +DBIx::Class::CDBICompat::Relationships - Emulate has_a(), has_many(), might_have() and meta_info() =head1 DESCRIPTION @@ -23,11 +24,29 @@ Emulate C, C, C and C. =cut sub has_a { + my($self, $col, @rest) = @_; + + $self->_declare_has_a($col, @rest); + $self->_mk_inflated_column_accessor($col); + + return 1; +} + + +sub _declare_has_a { my ($self, $col, $f_class, %args) = @_; - $self->throw_exception( "No such column ${col}" ) unless $self->has_column($col); + $self->throw_exception( "No such column ${col}" ) + unless $self->has_column($col); $self->ensure_class_loaded($f_class); - - my $rel; + + my $rel_info; + + # Class::DBI allows Non database has_a with implicit deflate and inflate + # Hopefully the following will catch Non-database tables. + if( !$f_class->isa('DBIx::Class::Row') and !$f_class->isa('Class::DBI::Row') ) { + $args{'inflate'} ||= sub { $f_class->new(shift) }; # implicit inflate by calling new + $args{'deflate'} ||= sub { shift() . '' }; # implicit deflate by stringification + } if ($args{'inflate'} || $args{'deflate'}) { # Non-database has_a if (!ref $args{'inflate'}) { @@ -39,24 +58,31 @@ sub has_a { $args{'deflate'} = sub { shift->$meth; }; } $self->inflate_column($col, \%args); - - $rel = { + + $rel_info = { class => $f_class }; } else { $self->belongs_to($col, $f_class); - $rel = $self->result_source_instance->relationship_info($col); + $rel_info = $self->result_source_instance->relationship_info($col); } - + + $rel_info->{args} = \%args; + $self->_extend_meta( has_a => $col, - $rel + $rel_info ); - + return 1; } +sub _mk_inflated_column_accessor { + my($class, $col) = @_; + + return $class->mk_group_accessors('inflated_column' => $col); +} sub has_many { my ($class, $rel, $f_class, $f_key, $args) = @_; @@ -90,30 +116,31 @@ sub has_many { $class->next::method($rel, $f_class, $f_key, $args); + my $rel_info = $class->result_source_instance->relationship_info($rel); + $args->{mapping} = \@f_method; + $args->{foreign_key} = $f_key; + $rel_info->{args} = $args; + $class->_extend_meta( has_many => $rel, - $class->result_source_instance->relationship_info($rel) + $rel_info ); if (@f_method) { - no strict 'refs'; - no warnings 'redefine'; - my $post_proc = sub { my $o = shift; $o = $o->$_ for @f_method; $o; }; - *{"${class}::${rel}"} = - sub { - my $rs = shift->search_related($rel => @_); - $rs->{attrs}{record_filter} = $post_proc; - return (wantarray ? $rs->all : $rs); - }; + quote_sub "${class}::${rel}", sprintf( <<'EOC', perlstring $rel), { '$rf' => \sub { my $o = shift; $o = $o->$_ for @f_method; $o } }; + my $rs = shift->search_related( %s => @_); + $rs->{attrs}{record_filter} = $rf; + return (wantarray ? $rs->all : $rs); +EOC + return 1; } - } sub might_have { my ($class, $rel, $f_class, @columns) = @_; - + my $ret; if (ref $columns[0] || !defined $columns[0]) { $ret = $class->next::method($rel, $f_class, @columns); @@ -121,26 +148,34 @@ sub might_have { $ret = $class->next::method($rel, $f_class, undef, { proxy => \@columns }); } - + + my $rel_info = $class->result_source_instance->relationship_info($rel); + $rel_info->{args}{import} = \@columns; + $class->_extend_meta( might_have => $rel, - $class->result_source_instance->relationship_info($rel) + $rel_info ); - + return $ret; } sub _extend_meta { my ($class, $type, $rel, $val) = @_; - my %hash = %{ Clone::clone($class->__meta_info || {}) }; + +### Explicitly not using the deep cloner as Clone exhibits specific behavior +### wrt CODE references - it simply passes them as-is to the new structure +### (without deparse/eval cycles). There likely is code that relies on this +### so we just let sleeping dogs lie. + my $hash = Clone::clone($class->__meta_info || {}); $val->{self_class} = $class; $val->{type} = $type; $val->{accessor} = $rel; - $hash{$type}{$rel} = DBIx::Class::CDBICompat::Relationship->new($val); - $class->__meta_info(\%hash); + $hash->{$type}{$rel} = DBIx::Class::CDBICompat::Relationship->new($val); + $class->__meta_info($hash); } @@ -166,11 +201,31 @@ sub search { : undef()); if (ref $where eq 'HASH') { foreach my $key (keys %$where) { # has_a deflation hack - $where->{$key} = ''.$where->{$key} - if eval { $where->{$key}->isa('DBIx::Class') }; + $where->{$key} = ''.$where->{$key} if ( + defined blessed $where->{$key} + and + $where->{$key}->isa('DBIx::Class') + ); } } $self->next::method($where, $attrs); } +sub new_related { + return shift->search_related(shift)->new_result(shift); +} + +=head1 FURTHER QUESTIONS? + +Check the list of L. + +=head1 COPYRIGHT AND LICENSE + +This module is free software L +by the L. You can +redistribute it and/or modify it under the same terms as the +L. + +=cut + 1;