开发者

Kohana orm order asc/desc?

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 t开发者_开发技巧aken ,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!


  1. Yes, you can sort resulting rows in ORM with order_by($column, $order). For example, ->order_by('id', 'ASC').

  2. 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); 
  }


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();


Doing like this, I suppose you'll be :

  • selecting all lines of your table that correspond to your condition
  • fetching all those lines from MySQL to PHP
  • to, finally, only work with one of those lines


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜