[Dbix-class] Extended JOIN syntax

Peter Rabbitson rabbit+dbic at rabbit.us
Sun Jan 29 20:56:59 GMT 2012


Bernhard Graf wrote:
> Am 29.01.2012 20:42, schrieb Bernhard Graf:
> 
>> The example above still requires the bind values to be defined
>> statically in the schema. I couldn't find any examples, about how to
>> specify bind values at execution time - e.g. likes this:
>>
>> 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" => { '>', \'?', '<', \'?' },
>> };
>> }
>> );
>>
>> ...
>>
>> $artist_rs->search_related('cds_era', {}, {bind => ['1979', 
>> '1990'])->next;
>>
>>
>> Isn't something like this possible?
> 
> Actually it does somehow!
> Caveat: { '>', \'?', '<', \'?' } is a hash ref und so the elements 
> appear in a random order, which is probably not the same as in the 
> bind-array. :-(
> 
> At least it works for this case:
> 
> My::Schema::Artist->has_many(
>   cds_year => 'My::Schema::CD',
>   sub {
>     my $args = shift;
> 
>     return {
>       "$args->{foreign_alias}.artist" => {-ident => 
> "$args->{self_alias}.artistid"},
>       "$args->{foreign_alias}.year"   => {-ident => '?'},
>     };
>   }
> );
> 
> $artist_rs->search_related('cds_year', {}, {bind => ['1990']})->next;
> 
> 

Just to make sure I was clear in my previous email - the above is
a horrible horrible idea and should never be done. The 'bind'
resultset attribute is a legacy kludge which (literally) means
"take this bag of bind values and prepend them to the stack of
bind values that the current from-spec has generated". In other
words the values specified there are blindly inserted between the
SELECT bindvars (think search ({}, { +select => \' column + ? '}))
and the programatically generated FROM bind values (think bind
values from joins-conds).

As such this is (as you already noticed) very fragile and
not-to-be-depended-on stuff. I am just adding this note for the
unfortunate googler in the future :)

Cheers



More information about the DBIx-Class mailing list