[Dbix-class] DBIx::Class Relationship Setup Based on Substring of Value in Field

stephenmoy smoy at stern.nyu.edu
Wed May 1 17:49:33 GMT 2013


Hi everybody,

Does DBIx::Class allow relationships to be set up between two fields that
are two different sizes?  We have a registration table and a class table
that are linked by term, course number, and section number.  However, the
section number in one table has three digits, while the other one has two.

I've tried suggestions in the "DBIx::Class has_many join on substring of a
column" web document, but to no avail.

Class DB Schema (field for section is 2 digits here, i.e.: '01'):
__PACKAGE__->has_many( reginfo => 'DB::Schema::Result::Registration',
              sub {
                 my $args = shift;
                 return (
                     {
                        "$args->{foreign_alias}.term" => { -ident =>
"$args->{self_alias}.term" },
                        "$args->{foreign_alias}.crs"  => { -ident =>
"$args->{self_alias}.crs" },
                        "$args->{foreign_alias}.sec"  => "0" .
$args->{self_alias}.sec,
                     },
                     $args->{self_rowobj} && {
                        "$args->{foreign_alias}.term" =>
$args->{self_rowobj}->term,
                        "$args->{foreign_alias}.crs"  =>
$args->{self_rowobj}->crs,
                        "$args->{foreign_alias}.sec"  => "0" .
$args->{self_rowobj}->sec,
                     },
                 )
             }
);

Registration DB Schema (field for section is 3 digits here, i.e.: '001'):
__PACKAGE__->has_one( class => 'DB::Schema::Result::Class',
             sub {
                 my $args = shift;
                 return (
                     {
                        "$args->{foreign_alias}.term"  => { -ident =>
"$args->{self_alias}.term" },
                        "$args->{foreign_alias}.crs" => { -ident =>
"$args->{self_alias}.crs" },
                        "$args->{foreign_alias}.sec" =>
substr($args->{self_alias}.sec, 1, 2),
                     },
                     $args->{self_rowobj} && {
                        "$args->{foreign_alias}.term" =>
$args->{self_rowobj}->term,
                        "$args->{foreign_alias}.crs" =>
$args->{self_rowobj}->crs,
                        "$args->{foreign_alias}.sec" =>
substr($args->{self_alias}.sec, 1, 2),
                     },
                 )
             }
);

If it's possible, any help will be greatly appreciated.




--
View this message in context: http://dbix-class.35028.n2.nabble.com/DBIx-Class-Relationship-Setup-Based-on-Substring-of-Value-in-Field-tp7578150.html
Sent from the DBIx-Class mailing list archive at Nabble.com.



More information about the DBIx-Class mailing list