如何在Kohana中实现多关系ORM?

时间:2012-06-17 10:00:40

标签: php orm kohana kohana-orm

我对Kohana ORM的关系有疑问。我有三种型号:用户,歌曲和标签。

User {
    has many Songs;
    has many Tags; (followed tags)
}

Song {
    belongs to User;
    has many Tags;
}

Tag {
    has many Users;
    has many Songs;
}

示例:

  • 用户' Naimad'以下是标签:Deadmau5和Inpetto。
  • 每个标签都有两首歌:
    1. Deadmau5标签有:Veldt和Strobe,
    2. Inpetto标签有:Toca的奇迹和风暴。

我想从标签中获取这些歌曲,然后是用户,我不知道该怎么做。我知道这是一个愚蠢的例子,但我在发布这个问题之前尝试过:

$songs = ORM::factory('user', array('name' => 'Naimad'))
    ->tags
    ->songs
    ->find_all();

1 个答案:

答案 0 :(得分:0)

我认为你需要使用 - > with('tag') - > with('song')

$songs = ORM::factory('user', array('name' => 'Naimad'))
    ->with('tags')
    ->with('songs')
    ->find_all();

然后访问该值对对象进行循环 $ s-> tags-> field_name ...等等