views:

116

answers:

1

Is there any way to sort in orm by using a value from a forgin table?

Can you do something like orm::factory("table")->order_by("table.foregin_table.column" , "ASC") or do you have to use some regular mysql and join the tables together old school?

+1  A: 

Of course it's possible. For example I have two tables with pictures and votes with relation one to many. Let's say I want to sort pictures by number of votes to get the most popular pictures. It will this:

$pictures = ORM::factory('picture')
    ->select(array('COUNT("picture_votes.id")', 'votes'))
    ->join('picture_votes','left')
    ->on('picture_votes.picture_id','=','pictures.id')
    ->group_by('pictures.id')
    ->order_by('votes','desc')
    ->find_all();

This will give all pictures sorted by number of votes as result.

Karol Janyst