Suppose we have two ORM model:
class Model_User extends ORM
{
protected $_has_many = array(
'orders' => array(
'model' => 'Order',
'foreign_key' => 'order_id',
),
);
}
class Model_Order extends ORM
{
protected $_belongs_to = array(
'author' => array(
'model' => 'User',
'foreign_key' => 'author_id',
),
);
}
ORM::factory('User')->find_all(); //Displays a list of all users
As you can see a list of all users, sorted by the number of orders? Is it possible?
I type this straight out my head, so sorry for eventual mistakes
ORM::factory('user')->join('orders', 'left')
->on('orders.id', '=', 'user.order_id')
->order_by(DB::expr('COUNT("orders.id")')
->find_all();