Kohana 3.1 ORM 关系问题

发布于 2024-11-14 14:43:19 字数 703 浏览 6 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城月光淡如水﹏ 2024-11-21 14:43:19

尝试使用 as_array

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

Try using as_array

$user->userrelations->find_all()->as_array('col1', 'col2');
荒岛晴空 2024-11-21 14:43:19

所以,它应该是这样的:

在用户模型中:

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

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

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文