}
}
return \%ret;
+ } elsif (ref $cond eq 'ARRAY') {
+ return [ map { $self->resolve_condition($_, $rel, $for) } @$cond ];
} else {
die("Can't handle this yet :(");
}
sub _join_condition {
my ($self, $cond) = @_;
- die "no chance" unless ref $cond eq 'HASH';
- my %j;
- for (keys %$cond) { my $x = '= '.$self->_quote($cond->{$_}); $j{$_} = \$x; };
- return $self->_recurse_where(\%j);
+ if (ref $cond eq 'HASH') {
+ my %j;
+ for (keys %$cond) { my $x = '= '.$self->_quote($cond->{$_}); $j{$_} = \$x; };
+ return $self->_recurse_where(\%j);
+ } elsif (ref $cond eq 'ARRAY') {
+ return join(' OR ', map { $self->_join_condition($_) } @$cond);
+ } else {
+ die "Can't handle this yet!";
+ }
}
sub _quote {
'FourKeys',
'#dummy',
'SelfRef',
+ 'ArtistUndirectedMap',
'Producer',
'CD_to_Producer',
),
--- /dev/null
+package DBICTest::Schema::ArtistUndirectedMap;
+
+use base 'DBIx::Class::Core';
+
+__PACKAGE__->table('artist_undirected_map');
+__PACKAGE__->add_columns(qw/id1 id2/);
+__PACKAGE__->set_primary_key(qw/id1 id2/);
+
+1;
onekeys => 'DBICTest::Schema::OneKey',
{ 'foreign.artist' => 'self.artistid' }
);
-
+DBICTest::Schema::Artist->add_relationship(
+ artist_undirected_maps => 'DBICTest::Schema::ArtistUndirectedMap',
+ [{'foreign.id1' => 'self.artistid'}, {'foreign.id2' => 'self.artistid'}],
+ { accessor => 'multi' }
+);
+DBICTest::Schema::ArtistUndirectedMap->add_relationship(
+ 'mapped_artists', 'DBICTest::Schema::Artist',
+ [{'foreign.artistid' => 'self.id1'}, {'foreign.artistid' => 'self.id2'}]
+);
DBICTest::Schema::CD->add_relationship(
artist => 'DBICTest::Schema::Artist',
{ 'foreign.artistid' => 'self.artist' },
DBICTest::Schema::TwoKeys->belongs_to('artist', 'DBICTest::Schema::Artist');
DBICTest::Schema::TwoKeys->belongs_to('cd', 'DBICTest::Schema::CD');
-DBICTest::Schema::CD_to_Producer->belongs_to('cd', 'DBICTest::Schema::CD', { 'foreign.cdid' => 'self.cd' });
-DBICTest::Schema::CD_to_Producer->belongs_to('producer', 'DBICTest::Schema::Producer', { 'foreign.producerid' => 'self.producer' });
+DBICTest::Schema::CD_to_Producer->belongs_to(
+ 'cd', 'DBICTest::Schema::CD',
+ { 'foreign.cdid' => 'self.cd' }
+);
+DBICTest::Schema::CD_to_Producer->belongs_to(
+ 'producer', 'DBICTest::Schema::Producer',
+ { 'foreign.producerid' => 'self.producer' }
+);
+DBICTest::Schema::Artist->has_many(
+ 'artist_undirected_maps', 'DBICTest::Schema::ArtistUndirectedMap',
+ [{'foreign.id1' => 'self.artistid'}, {'foreign.id2' => 'self.artistid'}]
+);
+DBICTest::Schema::ArtistUndirectedMap->belongs_to(
+ 'artist1', 'DBICTest::Schema::Artist', 'id1');
+DBICTest::Schema::ArtistUndirectedMap->belongs_to(
+ 'artist2', 'DBICTest::Schema::Artist', 'id2');
+DBICTest::Schema::ArtistUndirectedMap->has_many(
+ 'mapped_artists', 'DBICTest::Schema::Artist',
+ [{'foreign.artistid' => 'self.id1'}, {'foreign.artistid' => 'self.id2'}]);
# now the Helpers
DBICTest::Schema::CD->many_to_many( 'producers', 'cd_to_producer', 'producer');
CREATE TABLE self_ref_alias (self_ref INTEGER NOT NULL, alias INTEGER NOT NULL,
PRIMARY KEY( self_ref, alias ) );
+CREATE TABLE artist_undirected_map (id1 INTEGER NOT NULL, id2 INTEGER NOT NULL, PRIMARY KEY(id1, id2));
+
CREATE TABLE producer (producerid INTEGER NOT NULL PRIMARY KEY, name VARCHAR);
CREATE TABLE cd_to_producer (cd INTEGER NOT NULL, producer INTEGER NOT NULL);
INSERT INTO self_ref_alias (self_ref, alias) VALUES (1, 2);
+INSERT INTO artist_undirected_map (id1, id2) VALUES (1, 2);
+
INSERT INTO producer (producerid, name) VALUES (1, 'Matt S Trout');
INSERT INTO cd_to_producer (cd, producer) VALUES (1, 1);
sub run_tests {
-
-plan tests => 14;
+
+use strict;
+use warnings;
+plan tests => 17;
# has_a test
my $cd = DBICTest->class("CD")->find(4);
my @producers = $cd->producers();
is( $producers[0]->name, 'Matt S Trout', 'many_to_many ok' );
+# test undirected many-to-many relationship (e.g. "related artists")
+my $undir_maps = DBICTest->class("Artist")->find(1)->artist_undirected_maps;
+is($undir_maps->count, 1, 'found 1 undirected map for artist 1');
+
+$undir_maps = DBICTest->class("Artist")->find(2)->artist_undirected_maps;
+is($undir_maps->count, 1, 'found 1 undirected map for artist 2');
+
+my @art = $undir_maps->search_related('mapped_artists')->all;
+
+cmp_ok(@art, '==', 2, "Both artist returned from map");
+
}
1;