简体   繁体   中英

Sorting by foreign table value with Kohana ORM

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM