[Dbix-class] Handling Multiple join

Alexander Hartmaier alexander.hartmaier at t-systems.at
Thu Jan 20 09:50:35 GMT 2011


Hi linuxsupport,
join the #dbix-class irc channel for more direct help.

The point about an ORM is to *not* care about the sql.
If you just want sql generation you could use SQL::Abstract directly.
Think in objects and related objects, don't use select/as or columns
when you don't need to!

Example:

my $artist_rs = $schema->resultset('Artist')->search(undef, { prefetch
=> 'cds' });
while (my $artist = $artist_rs->next) {
    say $artist->name, ' released ', $artist->cds->count, ' cds:';
    say "\t", $_->name, ' (', $_->year, ')' for $artist->cds;
}

--
Best regards, Alex


On Fri, 2010-12-31 at 09:39 +0100, Wolfgang Kinkeldei wrote:
> Hi,
>
> Am 30.12.2010 um 06:45 schrieb linuxsupport:
>
> > Hi all,
> >
> > If I join multiple tables and one of the table contains more than 1 row and all records appear in more than 1 row.
> >
> > for example, table1 has id and create_date, table2 has author_name, table3 has gorup_name and these can return only one row for a given id, but table4 can have more than 1 record for given id ( id from table1 refers in every tables), so it return like
> >
> > id     |   create_date   | author_name    | group_name | comment_id
> > 1      |    2010-12-26   | name1             | group1          | 1
> > 1      |    2010-12-26   | name1             | group1          | 2 -- more than 1 comment_id for id -> 1
> >
> > Is there a way to tell search query to store the results of column commnet_id into another list or array? otherwise how to handle it?
>
> You could try to use DBIx::Class::ResultClass::HashRefInflator. If you prefetch your other tables, an array-ref for "has-many" related tables will be generated. However, the downside is that your retrieved "records" are not Objects anymore. Updates or Deletes will not work. You have been warned :-)
>
>
> Best,
>
> Wolfgang Kinkeldei
>


*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*
T-Systems Austria GesmbH   Rennweg 97-99, 1030 Wien
Handelsgericht Wien, FN 79340b
*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*
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