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) {...}