Kohana 3.1 ORM 关系问题
我正在使用 kohana3。
表: users id usernamefriend_id
user_relationships id user_idfriend_id
我正在尝试执行以下操作:
假设,我在 user_table 中有 id = 1。然后我想获取该用户的friend_id 数组。假设它是(2,3)。然后我想获取 id = (2,3)
用户模型的用户名:
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 receive ["id"]=> string(1) "1" ["user_id"]=>;字符串(1) "1" ["friend_id"]=>字符串(1)“2” 我应该写什么才能得到我想要的东西?
I'm using kohana3.
tables:
users id username friend_id
user_relationships 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用
as_array
Try using
as_array
所以,它应该是这样的:
在用户模型中:
我得到了这样所需的所有数据:
其中“朋友”是我在用户模型中使用的别名
So, it should be like this:
in user model:
and i get all data needed like this:
where 'friends' is alias i'm using in user model