问题
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.
回答1:
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();
回答2:
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();
来源:https://stackoverflow.com/questions/5685021/tables-not-joining-in-kohana-3-1-orm