CI活跃记录,逃脱& order_by datetime列

时间:2012-04-03 11:57:27

标签: sql codeigniter datetime activerecord sql-order-by

我注意到,当CI中的日期时间列使用活动记录进行排序时,它会将列视为字符串或int。

示例:

$this->db->limit(12);
    $this->db->where('subscribed',1);   
    $this->db->join('profiles','profiles.user_id=users.id');
    $this->db->where('active',1);
    $this->db->select('users.thumbUpload,users.vanity_url');
    $this->db->select('users.created_on as time');
    $this->db->order_by('time');
    $query = $this->db->get('users');

这就是users.created_on是一个日期时间字段。首先,是因为活动记录正在渲染time,还是其他东西?如果是的话,我可以通过某种方式阻止逃避订单吗?

另外,stackoverflow,请停止将'datetime'自动修正为'date time'。这很烦人。

干杯!

2 个答案:

答案 0 :(得分:0)

当你将第二个参数设置为false时,函数不会检查并转义字符串。试试这个

$this->db->select('users.created_on as time', FALSE);

或者您查询使用

$this->db->order_by('users.created_on', 'DESC'); //or ASC

对于复杂的查询

$this->db->query("query");

答案 1 :(得分:0)

根据CI(目前为2.2)的核心文件中的方法签名,它没有任何选项允许选择是否逃避。

// The original prototype of the order_by()
public function order_by($orderby, $direction = '') {
    // Definition
}

如您所见,参数列表中没有$escape = true参数。一种方法是破解这个核心文件我通常不会建议它,因为如果你以后将CI升级到更新的版本,那么这些更改将会丢失,但如果你不打算这样做,可以使用它)。

为此,首先将原型更改为:

public function order_by($orderby, $direction = '', $escape = true) {
    // Definition
}

然后检查定义的以下部分中的条件:

// Line 842
if($escape){
    $part = $this->_protect_identifiers(trim($part));
}else {
    $part = trim($part);
}

// Line 856
if($escape){
    $orderby = $this->_protect_identifiers($orderby);
}       

当你打电话时,为了防止逃跑:

$this->db->order_by($ORDERBY_CLAUSE, null, false);