【问题标题】:How to implement Multi relationship ORM in Kohana?如何在 Kohana 中实现多关系 ORM?
【发布时间】:2023-04-09 12:32:02
【问题描述】:

我对 Kohana ORM 中的关系有疑问。我有三个模型:User、Song 和 Tag。

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 标签有:托卡的奇迹和风暴。

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

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

【问题讨论】:

    标签: php orm kohana kohana-orm


    【解决方案1】:

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

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

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

    【讨论】:

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