arc_of_descent has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
I'm unable to figure this out. Any help would be great.
# search for im_network @search_msgs = $schema->resultset('Message')->search( { 'from_user.im_network' => 'Yahoo', }, { join => 'from_user' }, ); ok (scalar @search_msgs == 2, 'join message to user'); # search for receiving_user @search_msgs = $schema->resultset('Message')->search( { 'user.im_username' => 'linux_rohan', }, { join => { conversation => { 'users' => 'user' } } }, ); ok (scalar @search_msgs == 2, 'join message to users');
and the two SQL queries which are generated:
SELECT me.id, me.conversation, me.from_user, me.message, me.timestamp +FROM message me JOIN user from_user ON ( from_user.id = me.from_user + ) WHERE ( from_user.im_network = ? ): 'Yahoo' SELECT me.id, me.conversation, me.from_user, me.message, me.timestamp +FROM message me JOIN conversation conversation ON ( conversation.id += me.conversation ) LEFT JOIN conversation_user users ON ( users.conv +ersation = conversation.id ) LEFT JOIN user user ON ( user.id = users +.user ) WHERE ( user.im_username = ? ): 'linux_rohan'

The above code works great. Apologies for not divulging any pre information. I basically need a query which combines the above two joins.

I tried passing an arrayref to join, like this,

{ join => [ conversation => { 'users' => 'user' }, 'from_user', ]};

but this does not work and says "No such relationship users". I'm not sure I have the correct syntax.


--
Rohan

Replies are listed 'Best First'.
Re: DBIx::Class - A question on using joins
by arc_of_descent (Hermit) on Sep 11, 2007 at 19:48 UTC

    Ah. I really need to look closely.

    I need to run more tests, but I think the following should do.

    # search for both receiving_user and im_network @search_msgs = $schema->resultset('Message')->search( { 'user.im_username' => 'linux_rohan', 'user.im_network' => 'Yahoo', }, { join => { conversation => { 'users' => 'user' } } }, ); ok (scalar @search_msgs == 2, 'join message to users');

    Since I'm already joining into the user table, I can just search for im_network.


    --
    Rohan