[Dbix-class] DBIx::Class::Relationship::HasOne
Hartmaier Alexander
Alexander.Hartmaier at t-systems.at
Fri Sep 9 16:49:26 CEST 2005
Hi list!
I've just added this plugin to my local svn snapshot to make writing native dbic schema classes easier.
e.g instead of writing
__PACKAGE__->add_relationship(
cd => 'App::CD',
{ 'foreign.id_cd' => 'self.cd' },
);
only
__PACKAGE__->has_one('cd', 'App::CD');
is needed ;-)
Multiple key relationships are supported to with the following syntax:
__PACKAGE__->has_one('accessorname', 'Foreign::Class', {foreign_key1 => self_key1, foreign_key2 => self_key2});
As with all new things this needs extensive testing which I'll do next week but feel free to test it too ;-)
To enable 'has_one' here is the svn diff against the current snapshot:
*snip*
svn diff
Index: lib/DBIx/Class/CDBICompat/HasA.pm
===================================================================
--- lib/DBIx/Class/CDBICompat/HasA.pm (Revision 184)
+++ lib/DBIx/Class/CDBICompat/HasA.pm (Arbeitskopie)
@@ -22,9 +22,10 @@
my ($pri, $too_many) = keys %{ $f_class->_primaries };
$self->throw( "has_a only works with a single primary key; ${f_class} has more" )
if $too_many;
- $self->add_relationship($col, $f_class,
- { "foreign.${pri}" => "self.${col}" },
- { accessor => 'filter' } );
+ #$self->add_relationship($col, $f_class,
+ # { "foreign.${pri}" => "self.${col}" },
+ # { accessor => 'filter' } );
+ $self->has_one($col, $f_class);
return 1;
}
Index: lib/DBIx/Class/Relationship.pm
===================================================================
--- lib/DBIx/Class/Relationship.pm (Revision 184)
+++ lib/DBIx/Class/Relationship.pm (Arbeitskopie)
@@ -5,7 +5,7 @@
use base qw/DBIx::Class Class::Data::Inheritable/;
-__PACKAGE__->load_own_components(qw/Accessor CascadeActions ProxyMethods Base/);
+__PACKAGE__->load_own_components(qw/Accessor CascadeActions ProxyMethods Base HasOne/);
__PACKAGE__->mk_classdata('_relationships', { } );
*snip*
It already gets used by the CDBI compat layer for the has_a relation and passes all tests ;-)
And the new package:
*snip*
package DBIx::Class::Relationship::HasOne;
use strict;
use warnings;
sub has_one {
my ($class, $acc_name, $f_class, $conds, $args) = @_;
eval "require $f_class";
# single key relationship
if (not defined $conds && not defined $args) {
my ($pri, $too_many) = keys %{ $f_class->_primaries };
my $acc_type = ($class->_columns->{$acc_name}) ? 'filter' : 'single';
$class->add_relationship($acc_name, $f_class,
{ "foreign.${pri}" => "self.${acc_name}" },
{ accessor => $acc_type }
);
}
# multiple key relationship
else {
my %f_primaries = %{ $f_class->_primaries };
my $conds_rel;
for (keys %$conds) {
$conds_rel->{"foreign.$_"} = "self.".$conds->{$_};
# primary key usage checks
if (exists $f_primaries{$_}) {
delete $f_primaries{$_};
}
else
{
$class->throw("non primary key used in join condition: $_");
}
}
$class->throw("not all primary keys used in multi key relationship!") if keys %f_primaries;
$class->add_relationship($acc_name, $f_class,
$conds_rel,
{ accessor => 'single' }
);
}
return 1;
}
1;
*snip*
Looking forward for feedback!
With kind regards
Alexander Hartmaier
T-Systems Austria GesmbH
Rennweg 97-99 1030 Wien
phone: +43 57057-4320
fax: +43 57057-95-4320
mobile: +43 676 8642 4320
mail: alexander.hartmaier at t-systems.at
internet: http://www.t-systems.at
*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*
Hinweis: Dieses E-mail kann vertrauliche und geschützte Informationen enthalten.
Sollten Sie nicht der beabsichtigte Empfänger sein, verständigen Sie bitte den Absender und löschen Sie dieses E-mail dann sofort.
Notice: This e-mail contains information that is confidential and may be privileged.
If you are not the intended recipient, please notify the sender and then delete this e-mail immediately.
*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*
More information about the Dbix-class
mailing list