[Dbix-class] many_to_many and shortcut methods

Sven Eppler sven at sveneppler.de
Wed Jan 9 09:38:15 GMT 2008


Hello!

I currently work on getting my emails into a database. Therefore i have
following table setup:

Mails: id, body, received
MailsToHeaders: mailid, headerid, value
Headers: id, name

I have a many_to_many relationship established between the table Mails and
Headers over the linking table MailsToHeaders. No Problem sofar.

But what i currently want to do, is adding some shortcut methods to the
Mail-Results to simply do something like this:

my @mails = $schema->resultset("Mails")->search(undef, { prefetch => {
"mails_to_headers" => "headerid" });
# the prefetching returns all headers and their values to the
corresponding mail
foreach my $mail (@mails) {
        print $mail->to; // Return the value of the header with the name "To"
        print $mail->subject; // Return the value of the header with the
name "Subject"
}

I tried to play around with search_related. But this always executes an
new Query, what is not really needed (but i understand that DBIx::Class
may not be able to recognise this :) )

The only thing i'm currently thinking about is to iterate internaly in
these methods over my results und return the corresponding resultset. But
that doesn't look like a good solution to me.

So maybe somebody has an idea?

Thanks in advance,
Sven




More information about the DBIx-Class mailing list