Skip Menu |

This queue is for tickets about the DBIx-Class CPAN distribution.

Report information
The Basics
Id: 25412
Status: resolved
Priority: 0/
Queue: DBIx-Class

People
Owner: Nobody in particular
Requestors: mschwern [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Unimportant
Broken in: (no value)
Fixed in: (no value)



Subject: [PATCH] Fix has_many so it can infer the foreign key from a has_a in CDBI.
CDBI->has_many has the ability to infer the foreign key from an existing has_a relationship. This patch fixes the CDBI compat layer so it too can do the same. This can easily be pushed down into regular DBIx::Class. Sorry about the lame test classes, its late.
Subject: has_many.patch
=== t/cdbi-t/09-has_many.t ================================================================== --- t/cdbi-t/09-has_many.t (revision 27683) +++ t/cdbi-t/09-has_many.t (revision 27685) @@ -6,15 +6,15 @@ eval "use DBIx::Class::CDBICompat;"; plan skip_all => 'Class::Trigger and DBIx::ContextualFetch required' if $@; eval "use DBD::SQLite"; - plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 30); + plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 31); } use lib 't/testlib'; use Film; use Actor; -Film->has_many(actors => Actor => 'Film', { order_by => 'name' }); Actor->has_a(Film => 'Film'); +Film->has_many(actors => 'Actor', { order_by => 'name' }); is(Actor->primary_column, 'id', "Actor primary OK"); ok(Actor->can('Salary'), "Actor table set-up OK"); @@ -110,3 +110,18 @@ is($as->Name, 'Arnold Schwarzenegger', "Arnie's still Arnie"); + +# Test infering of the foreign key of a has_many from an existing has_a +{ + use Thing; + use OtherThing; + + Thing->has_a(that_thing => "OtherThing"); + OtherThing->has_many(things => "Thing"); + + my $other_thing = OtherThing->create({ id => 1 }); + Thing->create({ id => 1, that_thing => $other_thing }); + Thing->create({ id => 2, that_thing => $other_thing }); + + is_deeply [sort map { $_->id } $other_thing->things], [1,2]; +} === t/testlib/Thing.pm ================================================================== --- t/testlib/Thing.pm (revision 27683) +++ t/testlib/Thing.pm (revision 27685) @@ -0,0 +1,14 @@ +package Thing; +use base 'DBIx::Class::Test::SQLite'; + +Thing->set_table("thing"); +Thing->columns(All => qw(id that_thing)); + +sub create_sql { + return qq{ + id INTEGER, + that_thing INTEGER + }; +} + +1; === t/testlib/OtherThing.pm ================================================================== --- t/testlib/OtherThing.pm (revision 27683) +++ t/testlib/OtherThing.pm (revision 27685) @@ -0,0 +1,11 @@ +package OtherThing; +use base 'DBIx::Class::Test::SQLite'; + +OtherThing->set_table("other_thing"); +OtherThing->columns(All => qw(id)); + +sub create_sql { + return qq{ + id INTEGER + }; +} === lib/DBIx/Class/CDBICompat/HasMany.pm ================================================================== --- lib/DBIx/Class/CDBICompat/HasMany.pm (revision 27683) +++ lib/DBIx/Class/CDBICompat/HasMany.pm (revision 27685) @@ -20,6 +20,12 @@ $args->{cascade_delete} = 0; } + if( !$f_key and !@f_method ) { + my $f_source = $f_class->result_source_instance; + ($f_key) = grep { $f_source->relationship_info($_)->{class} eq $class } + $f_source->relationships; + } + $class->next::method($rel, $f_class, $f_key, $args); if (@f_method) {
On Tue Mar 13 10:00:40 2007, MSCHWERN wrote: Show quoted text
> CDBI->has_many has the ability to infer the foreign key from an
existing Show quoted text
> has_a relationship. This patch fixes the CDBI compat layer so it too > can do the same. > > This can easily be pushed down into regular DBIx::Class. > > Sorry about the lame test classes, its late.
I've applied this to the main trunk of DBIx::Class. Unfortunately we cant import it into the main DBIC as is, as it'll fail when there is more than one relation back to the current class. Jess
"resolved", at least once your patch set hits