[Dbix-class] Second join gets eaten in _merge_attr

brett gardner brett at clientcomm.com.au
Mon Oct 29 22:22:59 GMT 2007


I don't think its as simple as that.

I recently had to implement this behaviour and realised that in some 
cases you might need to rename the columns in the query. eg

$rs->search({'foo.bar_id' => 2},{join=>'foo'});

then later

my $new_rs = $rs->search({'foo.bar_id' = 45},{join=>'foo'});

which would result in the query

SELECT ....
FROM tbl1 LEFT JOIN foo ON (...)
LEFT JOIN foo foo2 ON (...)
WHERE
foo.bar_id = 2 AND foo.bar_id = 45

Which is incorrect. as the second search clause should be "foo2.bar_id = 
45".

Because the code may not be aware of how many times "foo" has been 
joined already the resultset needs to rename the search fields in the 
search method.

I overrode the "search" method and kept a count of how many times the 
table had been joined already and if the table had already been joined 
then I changed the search args, and the select, as, group_by and 
order_by attributes.

Brett


luke saunders wrote:
> my bad. fixed in trunk:
> http://dev.catalystframework.org/svnweb/bast/revision/?rev=3844
>
> thanks for the test.
>
> On 10/29/07, Zbigniew Lukasiak <zzbbyy at gmail.com> wrote:
>   
>> When you have a resultset that has a join in it and you try to do a
>> search on it with another join to the same table twice then the second
>> join is joined only once.
>>
>> # t/90join_torture.t
>>
>> my $rs = $schema->resultset("Artist")->search({}, { join => 'twokeys' });
>> my $second_search_rs = $rs->search({ 'cds_2.cdid' => '2' }, { join =>
>> ['cds', 'cds'] });
>> is(scalar(@{$second_search_rs->{attrs}->{join}}), 3, 'both joins kept');
>> ok($second_search_rs->next, 'query on double joined rel runs okay');
>>
>>
>> __OUTPUT__
>>
>> #   Failed test 'both joins kept'
>> #   at t/90join_torture.t line 125.
>> #          got: '2'
>> #     expected: '3'
>> DBIx::Class::ResultSet::next(): DBI Exception: DBD::SQLite::db
>> prepare_cached failed: no such column: cds_2.cdid(1) at dbdimp.c line
>> 271 [for Statement "SELECT me.artistid, me.name FROM artist me LEFT
>> JOIN twokeys twokeys ON ( twokeys.artist = me.artistid ) LEFT JOIN cd
>> cds ON ( cds.artist = me.artistid ) WHERE ( cds_2.cdid = ? )"] at
>> t/90join_torture.t line 126
>>
>>
>>
>> The svn diff is attached (it's against
>> http://dev.catalyst.perl.org/repos/bast/DBIx-Class/0.08/trunk/lib I
>> hope that's the right branch).
>>
>> --
>> Zbigniew Lukasiak
>> http://brudnopis.blogspot.com/
>>
>> _______________________________________________
>> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
>> IRC: irc.perl.org#dbix-class
>> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
>> Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.rawmode.org
>>
>>
>>     
>
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.rawmode.org
>
>
>   



More information about the DBIx-Class mailing list