简体   繁体   中英

How to chain several ORM relationships in Kohana 3?

fairly simple question I think that I couldn't find the answer to in the docs ( http://kohanaframework.org/guide/orm/relationships )

I have the need to define relationships with several Models (in bold) (each has a corresponding table) at once, if that makes sense.

I have several models that have slightly intertwined relationships.

the connection model $_belongs_to the user . This code works fine, great. But the connection also needs to $_has_one song (i can haz cheezeburger??), and to $has_one keyword , if that makes sense.

This is because I want to access the song name and keyword name through the connection, for instance using $connection->song->name , and then $ connection->keyword->name ...

SO, the problem is I can't seem to chain the relationships detailed above, as I can only declare one relationship per model.... so how do I do this? Or maybe there's another much easier way of doing it that I'm unaware of...

I presume I'm being a n00b and would love some help on this. Cheers.

You can collect relationships in arrays:

class Model_Connection extends ORM {

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

   protected $_has_one = array(
      'song' => array(),
      'keyword' => array(),
   );

}

PS. But it seems like both song and keyword must be in a $_belongs_to property (one song has many connections , one connection belongs to song ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM