2011-06-08 108 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');