Tables not joining in Kohana 3.1 ORM

旧城冷巷雨未停 提交于 2019-12-23 13:00:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!