Simple Is Good!

How To Web Dev

Kohana 3 loop through many to many ORM

Recently made the decision to start learning how to use Kohana 3. Been putting it off because 2.4 has done pretty much everything I need. But I have a project where I was getting frustrated by the limitations of ORM in 2.4 so I decided I needed to take the plunge into 3. Anyways, one of the first things I noticed is that I could not loop through one-to-many or many-to-many ORM objects like in 2.x.

Kohana 2.4 ORM loops didn’t work

foreach ($school->classes as $class) 
{
     echo $class->name;
}

ANd maybe i just wasn’t looking in the right place but I couldn’t find anywhere that showed how to loop, so here it is this is what I finally figured out.

Kohana 3 ORM Loop one-to-many or many-to-many

foreach ($school->classes->find_all() as $class)
{
     echo $class->name;
}

Now that wasn’t so hard. Hope this helps someone else out.

August 30, 2011 at 1:35 pm | Kohana, MySQL, PHP | No comment

Leave a Reply

Your email address will not be published. Required fields are marked *