Reputation: 1222
I have a users, messages and comments.
users id user_id
messages id user_id
comments id message_id
I can get users data of the message using with().
$model = ORM::factory('message');
$messages = $model
->with('user')
->find_all();
with('comment') doesn't work. I guess because it's created for one-to-one relations while I have message has_many comment.
How do I get comments data into $messages? Like the following: ['message_id']['comment_id'][DATA]?
Upvotes: 0
Views: 1281
Reputation: 5483
0. Define relationships:
User has_many Messages Message has_many Comments Message belongs_to User Comment belongs_to Message
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. Get user messages:
$messages = ORM::factory('user', $user_id)->messages->find_all();
foreach($messages as $message) {...}
2. Get message owner:
$user = ORM::factory('message', $message_id)->user; // without find_all()!
3. Get message comments:
$comments = ORM::factory('message', $message_id)->comments->find_all();
foreach($comments as $comment) {...}
Upvotes: 1