From: Guillermo Roditi Date: Tue, 16 Dec 2008 22:42:10 +0000 (+0000) Subject: rename option to undef_on_null_fk and make it default for belongs_to X-Git-Tag: v0.08240~219^2~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=cef1bddae9e76fe52c0ca064acea9fc961977d24;hp=c89815db08bb4cc35d69efefa8a27bf71ae13142;p=dbsrgits%2FDBIx-Class.git rename option to undef_on_null_fk and make it default for belongs_to --- diff --git a/Changes b/Changes index 4a114eb..9725474 100644 --- a/Changes +++ b/Changes @@ -3,9 +3,10 @@ Revision history for DBIx::Class - Classes submitted as result_class for a resultsource are now automatically loaded via ensure_loaded() - 'result_class' resultset attribute, identical to result_class() - - add 'any_null_means_no_value' option for relationship accessors of - type 'single'. This will prevent DBIC from querying the database - if one or more of the key columns IS NULL. Tests + docs (groditi) + - add 'null_on_fk' option for relationship accessors of type 'single'. + This will prevent DBIC from querying the database if one or more of + the key columns IS NULL. Tests + docs (groditi) + - for 'belongs_to' rels, 'null_on_fk' defaults to true. 0.08099_05 2008-10-30 21:30:00 (UTC) - Rewritte of Storage::DBI::connect_info(), extended with an diff --git a/lib/DBIx/Class/Relationship.pm b/lib/DBIx/Class/Relationship.pm index ce8a96f..3479ac2 100644 --- a/lib/DBIx/Class/Relationship.pm +++ b/lib/DBIx/Class/Relationship.pm @@ -208,12 +208,10 @@ Cascading deletes are off by default on a C relationship. To turn them on, pass C<< cascade_delete => 1 >> in the $attr hashref. -By default, DBIC will attempt to query the related table for a row when the -relationship accessor is called even if a foreign key member column IS NULL, -which can be wasteful. To avoid this query from being performed, pass -C<< any_null_means_no_value => 1 >> in the C<$attr> hashref. This only applies -to accessors of type 'single' (when your accessor and foreign key have -different names e.g. 'cd_id', and 'cd'). +By default, DBIC will return undef and avoid querying the database if a +C accessor is called when any part of the foreign key IS NULL. To +disable this behavior, pass C<< undef_on_null_fk => 0 >> in the C<$attr> +hashref. NOTE: If you are used to L relationships, this is the equivalent of C. diff --git a/lib/DBIx/Class/Relationship/Accessor.pm b/lib/DBIx/Class/Relationship/Accessor.pm index f23ec4c..ef38ff8 100644 --- a/lib/DBIx/Class/Relationship/Accessor.pm +++ b/lib/DBIx/Class/Relationship/Accessor.pm @@ -30,8 +30,8 @@ sub add_relationship_accessor { my $cond = $self->result_source->resolve_condition( $rel_info->{cond}, $rel, $self ); - if( exists $rel_info->{attrs}->{any_null_means_no_value} - && $rel_info->{attrs}->{any_null_means_no_value} ){ + if( exists $rel_info->{attrs}->{undef_on_null_fk} + && $rel_info->{attrs}->{undef_on_null_fk} ){ return if grep { not defined } values %$cond; } my $val = $self->find_related($rel, {}, {}); diff --git a/lib/DBIx/Class/Relationship/BelongsTo.pm b/lib/DBIx/Class/Relationship/BelongsTo.pm index 272b01b..eb10752 100644 --- a/lib/DBIx/Class/Relationship/BelongsTo.pm +++ b/lib/DBIx/Class/Relationship/BelongsTo.pm @@ -13,6 +13,8 @@ sub belongs_to { # assume a foreign key contraint unless defined otherwise $attrs->{is_foreign_key_constraint} = 1 if not exists $attrs->{is_foreign_key_constraint}; + $attrs->{undef_on_null_fk} = 1 + if not exists $attrs->{undef_on_null_fk}; # no join condition or just a column name if (!ref $cond) { diff --git a/t/66relationship.t b/t/66relationship.t index bfe7781..fe0196c 100644 --- a/t/66relationship.t +++ b/t/66relationship.t @@ -8,7 +8,7 @@ use DBICTest; my $schema = DBICTest->init_schema(); -plan tests => 73; +plan tests => 74; # has_a test my $cd = $schema->resultset("CD")->find(4); @@ -49,7 +49,9 @@ is( $big_flop_cd->title, 'Big Flop', 'create_related ok' ); $schema->storage->debugcb(sub { $queries++; }); $schema->storage->debug(1); $big_flop_cd->genre; #should not trigger a select query - is($queries, 0, 'No Select made for belongs_to if key IS NULL'); + is($queries, 0, 'No SELECT made for belongs_to if key IS NULL'); + $big_flop_cd->genre_inefficient; #should trigger a select query + is($queries, 1, 'SELECT made for belongs_to if key IS NULL when undef_on_null_fk disabled'); $schema->storage->debug(0); $schema->storage->debugcb(undef); } diff --git a/t/lib/DBICTest/Schema/CD.pm b/t/lib/DBICTest/Schema/CD.pm index 0c79e60..f222ff9 100644 --- a/t/lib/DBICTest/Schema/CD.pm +++ b/t/lib/DBICTest/Schema/CD.pm @@ -67,10 +67,22 @@ __PACKAGE__->belongs_to('genre', 'DBICTest::Schema::Genre', join_type => 'left', on_delete => 'SET NULL', on_update => 'CASCADE', - any_null_means_no_value => 1, }, ); +#This second relationship was added to test the short-circuiting of pointless +#queries provided by undef_on_null_fk. the relevant test in 66relationship.t +__PACKAGE__->belongs_to('genre_inefficient', 'DBICTest::Schema::Genre', + { 'foreign.genreid' => 'self.genreid' }, + { + join_type => 'left', + on_delete => 'SET NULL', + on_update => 'CASCADE', + undef_on_null_fk => 0, + }, +); + + #__PACKAGE__->add_relationship('genre', 'DBICTest::Schema::Genre', # { 'foreign.genreid' => 'self.genreid' }, # { 'accessor' => 'single' }