[Dbix-class] Error creating relationship at runtime

Jonas Alves jonas.alves at co.clix.pt
Thu Mar 16 15:17:03 CET 2006


I'm trying to create relationships at runtime using the AUTOLOAD method, 
but i'm having no luck.
Here is my class:

package My::Schema::Image;
use base 'DBIx::Class';
use strict;
use warnings;
use My::Config qw(:image);

our $AUTOLOAD;

__PACKAGE__->load_components(qw/Core/);
__PACKAGE__->table('image');
__PACKAGE__->add_columns(
   <snip>
);

sub AUTOLOAD {
     my $self = shift;
     (my $name = $AUTOLOAD) =~ s/.*://;
     if ($name =~ /^url(\d+)x(\d+)$/) {
         no strict 'refs';
         *{__PACKAGE__ . "::$name"} = _enq_url($1, $2);
         return $self->$name(@_);
     } elsif ($name =~ /^enq(\d+)x(\d+)$/) {
         warn "Creating Relationship '$name'";
         __PACKAGE__->add_relationship($name => 'Enq', {
               'foreign.image_id' => 'self.id',
               'foreign.width'  => $1, # I can do this because
               'foreign.height' => $2, # i hacked ResultSource.pm
         }, { accessor => 'single', join_type => 'LEFT' });
         #Class::C3->reinitialize;
         return $self->$name(@_); # this is line 141
     } elsif ($name eq 'DESTROY') { return }
     die "Unknown method '$AUTOLOAD'.";
}

sub _enq_url {
     my ($w, $h) = @_;
     my $name = "enq${w}x${h}";
     return sub {
         my $self = shift;
         my $enq = $self->$name;
         return "http://localhost/default.gif" unless $enq;
         return BASE_URL . $enq->path;
     }
}

1;

__END__

But then when I call

$img->enq82x82 i got the following error:

Creating Relationship 'enq82x82' at /lib/My/Schema/Image.pm line 133.
DBIx::Class::Relationship::Accessor::__ANON__(): No such relationship 
'enq82x82' at /lib/My/Schema/Image.pm line 141

The relationship is not being created. Why? Anyone has a clue?

--Jonas



More information about the Dbix-class mailing list