[Dbix-class] outer join with explicit criteria rears its ugly head (again)

michael reddick michael.reddick at gmail.com
Tue Oct 14 21:59:31 BST 2008


On Tue, Oct 14, 2008 at 11:51 AM, Jason Gottshall <jgottshall at capwiz.com>wr=
ote:

> Jason Gottshall wrote:
>
>> SELECT book.*, book_user.*
>> FROM book
>> LEFT OUTER JOIN book_user
>>  ON (    book_user.book_id =3D book.book_id
>>      AND book.user_id      =3D 'givenuser'
>>  )
>>
>
> michael reddick wrote:
>
>> The and "book.user_id =3D 'giveruser'" doesn't have to be in the ON
>> statement does it? You could just put it into the where clause and it sh=
ould
>> work the same.
>>
>
> Yeah, that's the first thing I tried, but it didn't work (on Oracle,
> anyway.) When I move the explicit criteria to the WHERE clause, I seem to
> lose the left-outer-ness of the join, and I only get back books for which
> the book_user table has a record. I'm stumped. Anyone else?
>

It's not returning all the books because now it's filtering only the ones
where book_user has a record for the user like you said. So it does seem
like you can only do it with the $user restriction in the ON clause.

I think you can do something like this but it's really ugly:

$c->model('DB::Book')->search({},
    { from =3D> [ {me =3D> 'book' },
        [ { 'book_user' =3D> 'book_user', -join_type =3D> 'left outer'},
          { 'book_user.book_id' =3D> 'me.book_id', 'book_user.user_id' =3D>
'$user'}
        ]
    ] } );


michael
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20081014/26e=
0c71c/attachment.htm


More information about the DBIx-Class mailing list