4

Is there any way to sort (or order by) in ORM by using a value from a foreign table?

Can you do something like the following:

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?

random
  • 9,774
  • 10
  • 66
  • 83
Navetz
  • 393
  • 1
  • 2
  • 12
  • 1
    `orm::factory("table")->with("table2")->order_by("table2.column" , "ASC")` should work (not tested). Have you tried this? – biakaveron Sep 10 '10 at 06:12

1 Answers1

6

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
  • 356
  • 2
  • 10