Having never worked with ORM before I thought I would give it a try today, I've come across my first problem that I don't know how to solve (due to my lack of understanding how ORM really works.)
Say I have three tables: Languages, Codes, and a pivot table Codes_Languages. These all have properly defined relationships in the models.
Now, to echo all the names of the languages, I could go like this:
$languages = ORM::factory('languages')->find_all();
foreach ($languages as $language)
{
echo $language->name, '<br />';
}
If I want to echo all the names of codes within a particular language, I could go like this:
$language = ORM::factory('languages', 1);
foreach ($language->codes as $code)
{
echo $code->title, ' ', $code->description;
}
But how about if I need to do this: Retrieve the latest five codes (ordered by DESC on code.time_posted) in each language?
$languages = ORM::factory('languages')->find_all();
???$languages->codes = ORM->order_by('time_posted', 'desc')->limit(5);???
foreach ($languages as $language)
{
echo $language->name, '<br />';
foreach ($language->codes as $code)
{
echo $code->name, ' ', $code->description;
}
}
I've placed question marks around some pseudo-code which I think explains what I'm trying to do. I've recently converted from CodeIngiter because I think Kohana is a bit more well thought out, however the docs seem lacking and I couldn't find out how to do what I needed.
Any guidance would be great. Thanks.