uhm. this is what I really need:
1. To fetch all the users
2. To fetch all the posts posted by each user
3. To fetch all the comments for each post
So I can do something like this:
foreach ($users as $user)
{
echo $user->name;
foreach ($user->posts as $post)
{
echo $post->content;
foreach ($post->comments as $comment)
{
echo $comment->content;
}
}
}
I know its possible to do that using lazy loading, but I wanted to use eager loading to do that. Is that really possible sir? Can I ask a little sample from you on how to do that? And nwei okay sir I will read the docs 😊 Thanks! 😊
EDIT:
I have read the docs:
As you see in has_and_belongs_to example, there are another option to set-up many-to-many relationship. This option exists, and could be your option whether your pivot table is not only used to linked two tables, but also have its own primary key and other columns. If you are in this situation, dont panic. You still can using the second option : through.
So, instead having has_and_belongs_to values, now your user model would be something like :
class User extends Gas {
public $relations = array(
'has_one' => array('wife' => array()),
'has_many' => array('kids' => array()),
'has_many' => array('job' => array(
'through' => 'job_user',
)),
);
}
But I still can’t understand on how will I achieve what I wanted in using that :(
So if u have time sir please kindly give me a sample/code snippet on how to achieve wanted. Thank you very much in advance.