简体   繁体   English

Codeigniter-如何对数据库结果对象进行排序(对php对象进行排序)

[英]Codeigniter - How can I sort my database result object (sorting php objects)

Apparently active record cannot deal with temporary tables - huge oversight in my opinion. 显然,活动记录无法处理临时表-我认为这是巨大的监督。 (ref: Codeigniter 2 active record — How do I create temporary table to apply second sort order? ) (参考: Codeigniter 2活动记录-如何创建临时表以应用第二排序顺序?

In light of this I am looking for a way to sort the result object produced by my query. 鉴于此,我正在寻找一种对查询产生的结果对象进行排序的方法。

Here is my query: 这是我的查询:

    $this->db
                ->select('*')
                ->from('item_categories')
                ->where('item_categories.cat_id', $c)
                ->or_where('item_categories.parent_id', $c)
                ->or_where('item_categories.gparent_id', $c)
                ->join('item_categories_rel', 'item_categories_rel.cat_id = item_categories.cat_id', 'right')
                ->join('item_entries', 'item_entries.item_id = item_categories_rel.item_id','right')
                ->join('ratings_total', 'ratings_total.item_id = item_entries.item_id')
                ->order_by("item_name", "asc")
                ->limit($l, $s);
            $result = $this->db->get();

So for example my results contain a field called site_rating which is of type decimal(2,1) , how can I sort my result object by site_rating in asc/desc order? 因此,例如,我的结果包含一个名为site_rating的字段,该字段的类型为decimal(2,1),我如何按site_rating和asc / desc的顺序对结果对象进行排序? Any help greatly appreciated. 任何帮助,不胜感激。

To be clear I do not want to change the current order, I want to apply a second sort order to the first result set - I have found I cannot do this with active record so would like to do it by sorting the result object if possible. 明确地说,我不想更改当前顺序,我想将第二个排序顺序应用于第一个结果集-我发现我无法使用活动记录来执行此操作,因此希望尽可能对结果对象进行排序。

 $this->db
                ->select('*')
                ->from('item_categories')
                ->where('item_categories.cat_id', $c)
                ->or_where('item_categories.parent_id', $c)
                ->or_where('item_categories.gparent_id', $c)
                ->join('item_categories_rel', 'item_categories_rel.cat_id = item_categories.cat_id', 'right')
                ->join('item_entries', 'item_entries.item_id = item_categories_rel.item_id','right')
                ->join('ratings_total', 'ratings_total.item_id = item_entries.item_id')
                ->order_by("site_rating", "DESC")
                ->limit($l, $s);
            $result = $this->db->get();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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