From: Matt S Trout Date: Mon, 2 Jan 2006 17:38:46 +0000 (+0000) Subject: Some tweaks to Componentised, many_to_many helper from abraxxa X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=7411204b288b1df6416832707a8a6ce11e2ab3d8;p=dbsrgits%2FDBIx-Class-Historic.git Some tweaks to Componentised, many_to_many helper from abraxxa --- diff --git a/MANIFEST b/MANIFEST index 425afd4..d8ae2be 100644 --- a/MANIFEST +++ b/MANIFEST @@ -52,6 +52,8 @@ lib/DBIx/Class/Relationship/BelongsTo.pm 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 @@ -135,10 +137,12 @@ t/lib/DBICTest/Schema.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 diff --git a/lib/DBIx/Class.pm b/lib/DBIx/Class.pm index ab9c53a..34b8661 100644 --- a/lib/DBIx/Class.pm +++ b/lib/DBIx/Class.pm @@ -7,6 +7,7 @@ use vars qw($VERSION); 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'; diff --git a/lib/DBIx/Class/Componentised.pm b/lib/DBIx/Class/Componentised.pm index 0b13100..2ee9094 100644 --- a/lib/DBIx/Class/Componentised.pm +++ b/lib/DBIx/Class/Componentised.pm @@ -15,7 +15,8 @@ sub inject_base { 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); } diff --git a/lib/DBIx/Class/Relationship.pm b/lib/DBIx/Class/Relationship.pm index 15f1bc6..3a68ee2 100644 --- a/lib/DBIx/Class/Relationship.pm +++ b/lib/DBIx/Class/Relationship.pm @@ -6,9 +6,7 @@ use warnings; use base qw/DBIx::Class/; __PACKAGE__->load_own_components(qw/ - HasMany - HasOne - BelongsTo + Helpers Accessor CascadeActions ProxyMethods @@ -100,6 +98,12 @@ except the implication is that the other object is always present. The only diff between C and C is that C uses an (ordinary) inner join, whereas C 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; diff --git a/lib/DBIx/Class/Row.pm b/lib/DBIx/Class/Row.pm index ac302ea..e00ee84 100644 --- a/lib/DBIx/Class/Row.pm +++ b/lib/DBIx/Class/Row.pm @@ -105,9 +105,6 @@ UPDATE query to commit any changes to the object to the db if required. 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; diff --git a/t/lib/DBICTest.pm b/t/lib/DBICTest.pm index ecd4350..6748bae 100755 --- a/t/lib/DBICTest.pm +++ b/t/lib/DBICTest.pm @@ -10,7 +10,7 @@ mkdir("t/var") unless -d "t/var"; 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); @@ -44,6 +44,10 @@ CREATE TABLE self_ref (id INTEGER NOT NULL PRIMARY KEY, 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'); @@ -108,11 +112,16 @@ INSERT INTO onekey (id, artist, cd) VALUES (2, 1, 2); 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); diff --git a/t/lib/DBICTest/Schema.pm b/t/lib/DBICTest/Schema.pm index d2a7e5b..941a8d4 100644 --- a/t/lib/DBICTest/Schema.pm +++ b/t/lib/DBICTest/Schema.pm @@ -21,6 +21,8 @@ __PACKAGE__->load_classes(qw/ 'FourKeys', '#dummy', 'SelfRef', + 'Producer', + 'CD_to_Producer', ), qw/SelfRefAlias CDWithArtist/ ); diff --git a/t/lib/DBICTest/Schema/BasicRels.pm b/t/lib/DBICTest/Schema/BasicRels.pm index f3483b5..13948af 100644 --- a/t/lib/DBICTest/Schema/BasicRels.pm +++ b/t/lib/DBICTest/Schema/BasicRels.pm @@ -36,7 +36,11 @@ DBICTest::Schema::CD->add_relationship( { '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', @@ -75,4 +79,16 @@ DBICTest::Schema::TwoKeys->add_relationship( { '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; diff --git a/t/lib/DBICTest/Schema/HelperRels.pm b/t/lib/DBICTest/Schema/HelperRels.pm index 63191ea..bfd331d 100644 --- a/t/lib/DBICTest/Schema/HelperRels.pm +++ b/t/lib/DBICTest/Schema/HelperRels.pm @@ -1,4 +1,4 @@ -package DBICTest::Schema::BasicRels; +package DBICTest::Schema::HelperRels; use base 'DBIx::Class::Core'; @@ -11,13 +11,13 @@ DBICTest::Schema::CD->belongs_to('artist', 'DBICTest::Schema::Artist'); 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'); @@ -27,11 +27,15 @@ DBICTest::Schema::SelfRef->has_many( 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; diff --git a/t/run/06relationship.tl b/t/run/06relationship.tl index 7141be8..ab35be0 100644 --- a/t/run/06relationship.tl +++ b/t/run/06relationship.tl @@ -1,6 +1,6 @@ sub run_tests { -plan tests => 13; +plan tests => 14; # has_a test my $cd = DBICTest->class("CD")->find(4); @@ -105,6 +105,11 @@ eval { }; 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;