[Dbix-class] Changing inner join to left join while defining $rs

Radoslaw Zielinski radek at eo.pl
Thu Jan 25 16:03:48 GMT 2007


Example case (pseudo-code):

CD->belongs_to('artist');
CD->might_have('pair' => 'CD' => 'other_cd_id');
Artist->has_many('cds' => 'CD' => 'artist_id');

Now, one might want to:

  $db->resultset('CD')->search( {}, { join => [ 'artist', { pair => 'artist' } ] } );

...which should generate:

  select * from cd
        join artist on artist.id=cd.id
        left join cd cd2 on cd.id=cd2.other_cd_id
        left join artist a2 on a2.id=cd2.artist_id;

...but currently the last join is not LEFT.  Test case attached.

-- 
Radosław Zieliński <radoslaw.zielinski at eo.pl>
eo Networks
-------------- next part --------------
=== t/60core.t
==================================================================
--- t/60core.t	(revision 8265)
+++ t/60core.t	(local)
@@ -134,7 +134,7 @@
 my $cd = $schema->resultset("CD")->find(1);
 my %cols = $cd->get_columns;
 
-cmp_ok(keys %cols, '==', 4, 'get_columns number of columns ok');
+cmp_ok(keys %cols, '==', 5, 'get_columns number of columns ok');
 
 is($cols{title}, 'Spoonful of bees', 'get_columns values ok');
 
@@ -150,7 +150,7 @@
 # check whether ResultSource->columns returns columns in order originally supplied
 my @cd = $schema->source("CD")->columns;
 
-is_deeply( \@cd, [qw/cdid artist title year/], 'column order');
+is_deeply( \@cd, [qw/cdid artist title year other_cd_id/], 'column order');
 
 $cd = $schema->resultset("CD")->search({ title => 'Spoonful of bees' }, { columns => ['title'] })->next;
 is($cd->title, 'Spoonful of bees', 'subset of columns returned correctly');
@@ -286,9 +286,9 @@
 
 # test remove_columns
 {
-  is_deeply([$schema->source('CD')->columns], [qw/cdid artist title year/]);
+  is_deeply([$schema->source('CD')->columns], [qw/cdid artist title year other_cd_id/]);
   $schema->source('CD')->remove_columns('year');
-  is_deeply([$schema->source('CD')->columns], [qw/cdid artist title/]);
+  is_deeply([$schema->source('CD')->columns], [qw/cdid artist title other_cd_id/]);
   ok(! exists $schema->source('CD')->_columns->{'year'}, 'year still exists in _columns');
 }
 
=== t/66relationship.t
==================================================================
--- t/66relationship.t	(revision 8265)
+++ t/66relationship.t	(local)
@@ -7,7 +7,7 @@
 
 my $schema = DBICTest->init_schema();
 
-plan tests => 56;
+plan tests => 57;
 
 # has_a test
 my $cd = $schema->resultset("CD")->find(4);
@@ -224,3 +224,6 @@
 cmp_ok($artist->cds->count, '==', 0, "Correct new #cds for artist");
 cmp_ok($nartist->cds->count, '==', 2, "Correct new #cds for artist");
 
+my $related_cds = $schema->resultset( 'CD' )->search_rs( {}, { 'join' => [ 'artist', { pair => 'artist' } ] } );
+cmp_ok( $related_cds->count, '==', 1, 'Correct related cds' );
+
=== t/lib/DBICTest/Schema/CD.pm
==================================================================
--- t/lib/DBICTest/Schema/CD.pm	(revision 8265)
+++ t/lib/DBICTest/Schema/CD.pm	(local)
@@ -20,6 +20,10 @@
     data_type => 'varchar',
     size      => 100,
   },
+  'other_cd_id' => {
+    data_type => 'integer',
+    is_auto_increment => 0,
+  },
 );
 __PACKAGE__->set_primary_key('cdid');
 __PACKAGE__->add_unique_constraint([ qw/artist title/ ]);
@@ -45,4 +49,6 @@
     { order_by => 'producer.name' },
 );
 
+__PACKAGE__->might_have( pair => 'DBICTest::Schema::CD' => 'other_cd_id' );
+
 1;
=== t/lib/DBICTest.pm
==================================================================
--- t/lib/DBICTest.pm	(revision 8265)
+++ t/lib/DBICTest.pm	(local)
@@ -112,12 +112,12 @@
     ]);
 
     $schema->populate('CD', [
-        [ qw/cdid artist title year/ ],
-        [ 1, 1, "Spoonful of bees", 1999 ],
-        [ 2, 1, "Forkful of bees", 2001 ],
-        [ 3, 1, "Caterwaulin' Blues", 1997 ],
-        [ 4, 2, "Generic Manufactured Singles", 2001 ],
-        [ 5, 3, "Come Be Depressed With Us", 1998 ],
+        [ qw/cdid artist title year other_cd_id/ ],
+        [ 1, 1, "Spoonful of bees", 1999, undef ],
+        [ 2, 1, "Forkful of bees", 2001, 1 ],
+        [ 3, 1, "Caterwaulin' Blues", 1997, undef ],
+        [ 4, 2, "Generic Manufactured Singles", 2001, undef ],
+        [ 5, 3, "Come Be Depressed With Us", 1998, undef ],
     ]);
 
     $schema->populate('LinerNotes', [
=== t/lib/sqlite.sql
==================================================================
--- t/lib/sqlite.sql	(revision 8265)
+++ t/lib/sqlite.sql	(local)
@@ -89,7 +89,8 @@
   cdid INTEGER PRIMARY KEY NOT NULL,
   artist integer NOT NULL,
   title varchar(100) NOT NULL,
-  year varchar(100) NOT NULL
+  year varchar(100) NOT NULL,
+  other_cd_id integer
 );
 
 --


More information about the Dbix-class mailing list