Reputation: 5208
I heed two variables storing the maximum id from a table, and the minimum id from the same table.
the first id is easy to be taken ,using find() and a query like
$first = Model::factory('product')->sale($sale_id)->find();
but how can i retrieve the last id? is there a sorting option in the Kohana 3 ORM? thanks!
Upvotes: 2
Views: 10790
Reputation: 356
The problem could actually be that you are setting order_by after find_all. You should put it before. People do tend to put it last. This way it works.
$smthn = ORM::factory('smthn')
->where('something', '=', something)
->order_by('id', 'desc')
->find_all();
Upvotes: 3
Reputation: 5483
Yes, you can sort resulting rows in ORM with order_by($column, $order)
. For example, ->order_by('id', 'ASC')
.
Use QBuilder to get a specific values:
public function get_minmax() { return DB::select(array('MAX("id")', 'max_id'),array('MIN("id")', 'min_id')) ->from($this->_table_name) ->execute($this->_db); }
Upvotes: 6
Reputation: 401022
Doing like this, I suppose you'll be :
Ideally, you should be doing an SQL query that uses the MAX()
or the MIN()
function -- a bit like this :
select max(your_column) as max_value
from your_table
where ...
Not sure how to do that with Kohana, but this topic on its forum looks interesting.
Upvotes: 2