【问题标题】:Kohana 3.1 ORM relationships questionKohana 3.1 ORM 关系问题
【发布时间】:2011-06-08 13:57:04
【问题描述】:

我正在使用 kohana3。

表格: 用户 id 用户名friend_id

user_relationships id user_idfriend_id

我正在尝试执行以下操作:

假设,我在 user_table 中有 id = 1。然后我想得到这个用户的朋友 ID 数组。假设它是(2,3)。然后我想获取 id = (2,3) 的用户名

用户模型:

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

用户关系模型:

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

控制器:

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

我只收到 ["id"]=> string(1) "1" ["user_id"]=> string(1) "1" ["friend_id"]=> string(1) "2" 我应该写什么才能得到我想要的?

【问题讨论】:

  • 用户有很多朋友,用户可以成为很多其他用户的朋友。应该是 has_many “通过” (HABTM) 关系吗?
  • 你是对的,biakaveron

标签: orm kohana kohana-3 kohana-orm


【解决方案1】:

尝试使用as_array

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

【讨论】:

    【解决方案2】:

    所以,应该是这样的:

    在用户模型中:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2011-03-24
      • 1970-01-01
      • 1970-01-01
      • 2013-01-27
      • 1970-01-01
      • 1970-01-01
      • 2011-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多