user256430
user256430

Reputation: 3633

Tables not joining in Kohana 3.1 ORM

How do I get this to work?

$stuff = ORM::factory('mytable')
    ->with('user')
    ->with('other_stuff')
    ->find_all();

I've got all of my relationships set up and everything seems to be working when I do other queries. However, in the query above it is not joining table users to mytable. I think it may be because there can be many users for one mytable.

In the reference there is a method called join() which I think I might need to use here, but they don't give any information on it, and the stuff I've searched for on here does not work.

When I try to use join instead of with, it tries to join the table, but it doesn't include any "join on" information, just gives an empty ().

I know my ORM DB relationships are all set up correctly, so I'm a bit baffled.

Upvotes: 4

Views: 3552

Answers (2)

naveen
naveen

Reputation: 11

SELECT * from table1
LEFT JOIN table2
ON table1.id = table2.id
AND table2.flag = 'Y' 
AND table2.siteid = '12'
WHERE table1.siteid = '12'

How the above query is written in ORM format of kohana? Is the below one is correct

$stuff = ORM::factory('table1')
    ->join('table2','LEFT')
    ->on('table1.id','=','table2.id')
    ->on('table2.flag','=','Y')
    ->on('table2.siteid', '=', '12')
    ->where('table1.id', '=', '12')
    ->find_all();

Upvotes: 1

Kemo
Kemo

Reputation: 7042

Kohana has decent documentation, not looking in the right place is ... well, your problem.

ORM::with() is used for loading one-to-one (belongs to and has one) relations, though you have all the Database_Query_Builder methods to use with ORM on your disposal:

$stuff = ORM::factory('mytable')
        ->join('users','LEFT')
        ->on('users.mytable_id','=','mytables.id')
        ->find_all();

Upvotes: 5

Related Questions