2011-06-08 106 views
1

我正在使用kohana3。Kohana 3.1 ORM关系问题

表: 用户ID的用户名friend_id

user_relationships ID user_ID的friend_id

我试图做到以下几点:

假设,我有ID = 1 USER_TABLE。然后我想为这个用户获取一个friend_id数组。假设它会是(2,3)。然后我想为ID的用户名=(2,3)

用户模型:

protected $_has_many = array(
    'userrelations' => array() 
); 

userrelationships型号:

protected $_belongs_to = array(
    'user' => array(), 
); 

控制器:

$user = ORM::factory('user', 1); 
$friends = $user->userrelations->find_all(); 

我只recive [“id”] => string(1)“1”[“user_id”] => string(1)“1”[“friend_id”] => string(1)“2” 我写信得到我想要的东西?

+0

用户有很多朋友,用户可以成为很多其他用户的朋友。它是否应该通过“HABTM”关系? – biakaveron 2011-06-08 18:26:34

+0

你是对的,比卡凯龙 – Fenec 2011-06-09 09:13:00

回答

0

因此,尝试,它应该是这样的:

用户模型

'friends' => array('model' => 'user', 'through' => 'user_relationships') 

,我得到所需的所有数据是这样的:

$friends = $user->friends->find_all(); 

其中“朋友”是我在用户模型中使用的别名

0

使用as_array

$user->userrelations->find_all()->as_array('col1', 'col2');