- 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
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<belongs_to> 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<Class::DBI> relationships, this is the equivalent
of C<has_a>.
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, {}, {});
# 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) {
my $schema = DBICTest->init_schema();
-plan tests => 73;
+plan tests => 74;
# has_a test
my $cd = $schema->resultset("CD")->find(4);
$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);
}
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' }