Re: extra constraints on has_many relationship (patch)

[prev] [thread] [next] [Date index for 2004/10/25]

From: Cees Hek
Subject: Re: extra constraints on has_many relationship (patch)
Date: 01:58 on 25 Oct 2004
------=_Part_288_8010853.1098669499733
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

On Sun, 24 Oct 2004 17:08:51 +0100, Tony Bowden <tony-cdbitalk@xxxxx.xxx> wrote:
> Looks good to me. Tests welcome!

OK, here is an updated patch that includes tests.  I have also
expanded the patch slightly, so that the add_to_ method will "Do the
Right Thing".   ie with the following has_many relationship:

Director->has_many(r_rated_films => Film => 'Director', { constraint
=> { Rating => 'R' } });

using add_to_r_rated_films will automatically fill in Rating = 'R' for
you, unless Rating is already passed in by the user.

        -- 
        Cees Hek

------=_Part_288_8010853.1098669499733
Content-Type: text/x-patch; name="Class-DBI-0.96_hasmany_constraints.patch"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment; filename="Class-DBI-0.96_hasmany_constraints.patch"

diff -ru Class-DBI-0.96.orig/lib/Class/DBI/Relationship/HasMany.pm Class-DB=
I-0.96/lib/Class/DBI/Relationship/HasMany.pm
--- Class-DBI-0.96.orig/lib/Class/DBI/Relationship/HasMany.pm=092004-04-26 =
01:33:36.000000000 +1000
+++ Class-DBI-0.96/lib/Class/DBI/Relationship/HasMany.pm=092004-10-25 11:30=
:47.000000000 +1000
@@ -86,6 +86,12 @@
 =09=09my ($f_class, $f_key, $args) =3D
 =09=09=09($meta->foreign_class, $meta->args->{foreign_key}, $meta->args);
 =09=09$data->{$f_key} =3D $self->id;
+=09=09# See if has_many constraints were defined and auto fill them
+=09=09if (defined $args->{constraint} && ref $args->{constraint} eq 'HASH'=
) {
+=09=09=09while (my($k,$v) =3D each %{$args->{constraint}}) {
+=09=09=09=09$data->{$k} =3D $v unless defined $data->{$k};
+=09=09=09}
+=09=09}
 =09=09$f_class->create($data);
 =09};
 }
@@ -112,6 +118,7 @@
 =09=09my ($f_class, $f_key, $args) =3D
 =09=09=09($meta->foreign_class, $meta->args->{foreign_key}, $meta->args);
 =09=09if (ref $self) {    # For $artist->cds
+=09=09=09unshift @search_args, %{$args->{constraint}} if defined $args->{c=
onstraint} && ref $args->{constraint} eq 'HASH';
 =09=09=09unshift @search_args, ($f_key =3D> $self->id);
 =09=09=09push @search_args, { order_by =3D> $args->{order_by} }
 =09=09=09=09if defined $args->{order_by};
diff -ru Class-DBI-0.96.orig/lib/Class/DBI.pm Class-DBI-0.96/lib/Class/DBI.=
pm
--- Class-DBI-0.96.orig/lib/Class/DBI.pm=092004-04-30 17:22:12.000000000 +1=
000
+++ Class-DBI-0.96/lib/Class/DBI.pm=092004-10-25 11:42:50.000000000 +1000
@@ -2372,6 +2372,15 @@
 additional key/value pairs for restricting the search. The above example
 will only return the CDs with a year of 1980.
=20
+=09Music::Artist->has_many(rockcds =3D> 'Music::CD', { constraint =3D> [ g=
enre =3D> 'rock' ] });
+=09my @cds =3D $artist->rockcds(year =3D> 1980);
+
+You can also constrain the relationship to only include a subset of the=20
+available records by providing a 'constraint' option to the has_many
+relationship.  This will automatically add the constraint into the where
+clause when doing a search.  Also, the add_to_rockcds method will
+automatically fill in the constraint fields for you when creating a new CD=
.
+
 =3Dhead3 Ordering
=20
 =09Music::CD->has_many(tracks =3D> 'Music::Track', { order_by =3D> 'playor=
der' });
diff -ru Class-DBI-0.96.orig/t/09-has_many.t Class-DBI-0.96/t/09-has_many.t
--- Class-DBI-0.96.orig/t/09-has_many.t=092004-04-26 01:33:36.000000000 +10=
00
+++ Class-DBI-0.96/t/09-has_many.t=092004-10-25 11:55:28.000000000 +1000
@@ -3,13 +3,17 @@
=20
 BEGIN {
 =09eval "use DBD::SQLite";
-=09plan $@ ? (skip_all =3D> 'needs DBD::SQLite for testing') : (tests =3D>=
 30);
+=09plan $@ ? (skip_all =3D> 'needs DBD::SQLite for testing') : (tests =3D>=
 46);
=20
 =09use lib 't/testlib';
+=09use Director;
 =09use Film;
 =09use Actor;
+=09Director->CONSTRUCT;
 =09Film->CONSTRUCT;
 =09Actor->CONSTRUCT;
+=09Director->has_many(films =3D> Film =3D> 'Director', { order_by =3D> 'ti=
tle' });
+=09Director->has_many(r_rated_films =3D> Film =3D> 'Director', { order_by =
=3D> 'title', constraint =3D> { Rating =3D> 'R' } });
 =09Film->has_many(actors =3D> Actor =3D> 'Film', { order_by =3D> 'name' })=
;
 =09Actor->has_a(Film =3D> 'Film');
 =09is(Actor->primary_column, 'id', "Actor primary OK");
@@ -106,3 +110,67 @@
=20
 is($as->Name, 'Arnold Schwarzenegger', "Arnie's still Arnie");
=20
+
+
+ok(
+=09my $director =3D Director->create(
+=09=09{
+=09=09=09Name     =3D> 'Director 1',
+=09=09}
+=09),
+=09'create Director'
+);
+
+ok(
+=09$director->add_to_films(
+=09=09{
+=09=09=09Title    =3D> 'Film 1',
+=09=09=09Director =3D> 'Director 1',
+=09=09=09Rating   =3D> 'R',
+=09=09}
+=09),
+=09'add_to_films'
+);
+
+ok(
+=09$director->add_to_r_rated_films(
+=09=09{
+=09=09=09Title    =3D> 'Film 2',
+=09=09=09Director =3D> 'Director 1',
+=09=09}
+=09),
+=09'add_to_r_rated_films'
+);
+
+ok(
+=09$director->add_to_r_rated_films(
+=09=09{
+=09=09=09Title    =3D> 'Film 3',
+=09=09=09Director =3D> 'Director 1',
+=09=09=09Rating   =3D> 'G',
+=09=09}
+=09),
+=09'add_to_r_rated_films'
+);
+
+
+{
+=09my @films =3D $director->films;
+=09is(@films, 3, "Director 1 has three films");
+=09is($films[0]->Title, "Film 1", " - the correct title");
+=09is($films[0]->Rating, "R", " - the correct rating");
+=09is($films[1]->Title, "Film 2", " - the correct title");
+=09is($films[1]->Rating, "R", " - the correct rating");
+=09is($films[2]->Title, "Film 3", " - the correct title");
+=09is($films[2]->Rating, "G", " - the correct rating");
+}
+
+{
+=09my @films =3D $director->r_rated_films;
+=09is(@films, 2, "Director 1 has two R rated film");
+=09is($films[0]->Title, "Film 1", " - the correct title");
+=09is($films[0]->Rating, "R", " - the correct rating");
+=09is($films[1]->Title, "Film 2", " - the correct title");
+=09is($films[1]->Rating, "R", " - the correct rating");
+}
+

------=_Part_288_8010853.1098669499733--

Re: extra constraints on has_many relationship (patch)
Cees Hek 01:58 on 25 Oct 2004

Generated at 11:35 on 01 Dec 2004 by mariachi v0.52