我正在更新旧网站。我正在做的一件事是重写数据库查询,因为我已经更改了数据库的结构,使其更加灵活,并且可以从管理员那里进行配置。
我之前使用了以下查询(以及许多其他类似的查询),它依赖于一个临时表:
$query = $this->db->query("SELECT * FROM (SELECT * FROM Links WHERE Cat LIKE ('$c') AND Type LIKE ('%$x%') LIMIT $s, $l) AS T ORDER BY $sort $o");
因为查询变得更加复杂并且现在涉及很多连接,所以我决定使用活动记录语法来使它们更易于阅读。问题是我找不到有关如何制作临时表以使用活动记录对数据应用第二次排序的任何信息;这肯定是可能的吗?
这是我的新查询:
$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);
// up to here I want to store as a temporary table then apply next order
//->order_by($sort, $o); - ideally I want to apply a second order like this
$result = $this->db->get();
我想应用“order by”两次,但这不起作用——活动记录不是那种切割器(或者更可能是模棱两可的),我怎样才能在活动记录中做到这一点?
任何帮助深表感谢。如果活动记录方法失败,谁能建议我如何对结果对象应用排序?