Kohana 3.1 ORM relationships question
I'm using kohana3.
tables: users id username friend_id
user_relationship开发者_运维知识库s id user_id friend_id
I'm trying to do the following:
Let's assume, I have id = 1 in user_table. Then I want to get array of friend_id for this user. Assume it would be (2,3). And then I want to get username for id = (2,3)
user model:
protected $_has_many = array(
'userrelations' => array()
);
userrelationships model:
protected $_belongs_to = array(
'user' => array(),
);
controller:
$user = ORM::factory('user', 1);
$friends = $user->userrelations->find_all();
I only recive ["id"]=> string(1) "1" ["user_id"]=> string(1) "1" ["friend_id"]=> string(1) "2" What should I write to get what I want?
Try using as_array
$user->userrelations->find_all()->as_array('col1', 'col2');
So, it should be like this:
in user model:
'friends' => array('model' => 'user', 'through' => 'user_relationships')
and i get all data needed like this:
$friends = $user->friends->find_all();
where 'friends' is alias i'm using in user model
精彩评论