0. 関係を定義します。
ユーザー has_many メッセージ メッセージ has_many コメント メッセージ belongs_to ユーザー コメント belongs_to メッセージ
class Model_User extends ORM {
protected $_has_many = array('messages' => array());
}
class Model_Message extends ORM {
protected $_belongs_to = array('user' => array());
protected $_has_many = array('comments' => array());
}
class Model_Comment extends ORM {
protected $_belongs_to = array('message' => array());
}
1. ユーザー メッセージを取得します。
$messages = ORM::factory('user', $user_id)->messages->find_all();
foreach($messages as $message) {...}
2. メッセージの所有者を取得します。
$user = ORM::factory('message', $message_id)->user; // without find_all()!
3. メッセージのコメントを取得します。
$comments = ORM::factory('message', $message_id)->comments->find_all();
foreach($comments as $comment) {...}