[Dbix-class] Extended JOIN syntax

Peter Rabbitson rabbit+dbic at rabbit.us
Wed Feb 1 09:19:38 GMT 2012


Bernhard Graf wrote:
> Am 29.01.2012 21:52, schrieb Peter Rabbitson:
> 
>> It is possible, but is not yet particularly pretty. The trick is
>> that the coderef in question is called every time the relationship
>> needs to be resolved. By referencing some external package-global
>> you can control what the coderef will return.
> 
> like this?
> 
> __PACKAGE__->mk_group_accessors(inherited => qw(era_begin era_end));
> 
> My::Schema::Artist->has_many(
>   cds_era => 'My::Schema::CD',
>   sub {
>     my $args = shift;
> 
>     return {
>       "$args->{foreign_alias}.artist" => { -ident => 
> "$args->{self_alias}.artistid" },
>       "$args->{foreign_alias}.year"   => {
>          '>', __PACKAGE__->era_begin,
>          '<', __PACKAGE__->era_end,
>        },
>     };
>   }
> );
> 
> ...
> 
> $artist_rs->era_begin(1979);
> $artist_rs->era_end(1990);
> $artist_rs->search_related('cds_era')->next;
> 
> 

That's one way, though I would go for the more blunt:
   local $Some::Package::cond = 'foo'

Cheers



More information about the DBIx-Class mailing list