lib/DBIx/Class/Relationship/CascadeActions.pm
lib/DBIx/Class/Relationship/HasMany.pm
lib/DBIx/Class/Relationship/HasOne.pm
+lib/DBIx/Class/Relationship/Helpers.pm
+lib/DBIx/Class/Relationship/ManyToMany.pm
lib/DBIx/Class/Relationship/ProxyMethods.pm
lib/DBIx/Class/ResultSet.pm
lib/DBIx/Class/ResultSetInstance.pm
t/lib/DBICTest/Schema/Artist.pm
t/lib/DBICTest/Schema/BasicRels.pm
t/lib/DBICTest/Schema/CD.pm
+t/lib/DBICTest/Schema/CD_to_Producer.pm
t/lib/DBICTest/Schema/FourKeys.pm
t/lib/DBICTest/Schema/HelperRels.pm
t/lib/DBICTest/Schema/LinerNotes.pm
t/lib/DBICTest/Schema/OneKey.pm
+t/lib/DBICTest/Schema/Producer.pm
t/lib/DBICTest/Schema/SelfRef.pm
t/lib/DBICTest/Schema/SelfRefAlias.pm
t/lib/DBICTest/Schema/Tag.pm
use base qw/DBIx::Class::Componentised Class::Data::Accessor/;
sub mk_classdata { shift->mk_classaccessor(@_); }
+sub component_base_class { 'DBIx::Class' }
$VERSION = '0.04999_02';
sub load_components {
my $class = shift;
- my @comp = map { "DBIx::Class::$_" } grep { $_ !~ /^#/ } @_;
+ my $base = $class->component_base_class;
+ my @comp = map { "${base}::$_" } grep { $_ !~ /^#/ } @_;
$class->_load_components(@comp);
}
use base qw/DBIx::Class/;
__PACKAGE__->load_own_components(qw/
- HasMany
- HasOne
- BelongsTo
+ Helpers
Accessor
CascadeActions
ProxyMethods
between C<has_one> and C<might_have> is that C<has_one> uses an (ordinary) inner join,
whereas C<might_have> uses a left join.
+
+=head2 many_to_many
+
+ __PACKAGE__->many_to_many( 'accessorname' => 'a_to_b', 'table_b' );
+ my @f_objs = $obj_a->accessorname;
+
=cut
1;
sub update {
my ($self, $upd) = @_;
$self->throw( "Not in database" ) unless $self->in_storage;
- if (ref $upd eq 'HASH') {
- $self->$_($upd->{$_}) for keys %$upd;
- }
my %to_update;
$to_update{$_} = $self->get_column($_) for $self->is_changed;
return -1 unless keys %to_update;
my $dsn = "dbi:SQLite:${db_file}";
-DBICTest::Schema->compose_connection('DBICTest' => "dbi:SQLite:${db_file}");
+DBICTest::Schema->compose_connection('DBICTest' => $dsn);
my $dbh = DBI->connect($dsn);
CREATE TABLE self_ref_alias (self_ref INTEGER NOT NULL, alias INTEGER NOT NULL,
PRIMARY KEY( self_ref, alias ) );
+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 artist (artistid, name) VALUES (1, 'Caterwauler McCrae');
INSERT INTO artist (artistid, name) VALUES (2, 'Random Boy Band');
INSERT INTO onekey (id, artist, cd) VALUES (3, 2, 2);
-INSERT INTO self_ref(id, name) VALUES (1, 'First');
+INSERT INTO self_ref (id, name) VALUES (1, 'First');
+
+INSERT INTO self_ref (id, name) VALUES (2, 'Second');
+
+INSERT INTO self_ref_alias (self_ref, alias) VALUES (1, 2);
+
+INSERT INTO producer (producerid, name) VALUES (1, 'Matt S Trout');
-INSERT INTO self_ref(id, name) VALUES (2, 'Second');
+INSERT INTO cd_to_producer (cd, producer) VALUES (1, 1);
-INSERT INTO self_ref_alias(self_ref, alias) VALUES (1, 2);
EOSQL
$dbh->do($_) for split(/\n\n/, $sql);
'FourKeys',
'#dummy',
'SelfRef',
+ 'Producer',
+ 'CD_to_Producer',
),
qw/SelfRefAlias CDWithArtist/
);
{ 'foreign.liner_id' => 'self.cdid' },
{ join_type => 'LEFT' }
);
-
+DBICTest::Schema::CD->add_relationship(
+ cd_to_producer => 'DBICTest::Schema::CD_to_Producer',
+ { 'foreign.cd' => 'self.cdid' },
+ { join_type => 'LEFT', cascade_delete => 1 }
+);
DBICTest::Schema::SelfRefAlias->add_relationship(
self_ref => 'DBICTest::Schema::SelfRef',
{ 'foreign.cdid' => 'self.cd' }
);
+DBICTest::Schema::CD_to_Producer->add_relationship(
+ cd => 'DBICTest::Schema::CD',
+ { 'foreign.cdid' => 'self.cd' }
+);
+DBICTest::Schema::CD_to_Producer->add_relationship(
+ producer => 'DBICTest::Schema::Producer',
+ { 'foreign.producerid' => 'self.producer' }
+);
+
+# now the Helpers
+DBICTest::Schema::CD->many_to_many( 'producers', 'cd_to_producer', 'producer');
+
1;
-package DBICTest::Schema::BasicRels;
+package DBICTest::Schema::HelperRels;
use base 'DBIx::Class::Core';
DBICTest::Schema::CD->has_many(tracks => 'DBICTest::Schema::Track');
DBICTest::Schema::CD->has_many(tags => 'DBICTest::Schema::Tag');
+DBICTest::Schema::CD->has_many(cd_to_producer => 'DBICTest::Schema::CD_to_Producer' => 'cd');
DBICTest::Schema::CD->might_have(liner_notes => 'DBICTest::Schema::LinerNotes',
undef, { proxy => [ qw/notes/ ] });
DBICTest::Schema::SelfRefAlias->belongs_to(
self_ref => 'DBICTest::Schema::SelfRef');
-
DBICTest::Schema::SelfRefAlias->belongs_to(
alias => 'DBICTest::Schema::SelfRef');
DBICTest::Schema::Tag->belongs_to('cd', 'DBICTest::Schema::CD');
DBICTest::Schema::Track->belongs_to('cd', 'DBICTest::Schema::CD');
-
DBICTest::Schema::Track->belongs_to('disc', 'DBICTest::Schema::CD', 'cd');
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' });
+
+# now the Helpers
+DBICTest::Schema::CD->many_to_many( 'producers', 'cd_to_producer', 'producer');
+
1;
sub run_tests {
-plan tests => 13;
+plan tests => 14;
# has_a test
my $cd = DBICTest->class("CD")->find(4);
};
like($@, qr/join condition/, 'failed when creating a rel without join condition, ok');
+# many_to_many helper test
+$cd = DBICTest->class("CD")->find(1);
+my @producers = $cd->producers();
+is( $producers[0]->name, 'Matt S Trout', 'many_to_many ok' );
+
}
1;