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) {