2013-02-04 36 views
1

最近,我再次注意到CodeIgniter的ActiveRecord庫中一個令人難以置信的邏輯錯誤。Buggy加入CodeIgniter ActiveRecord

當我嘗試在連接中使用「AND」時會出現問題。

實例查詢:

$this -> db -> join(
    'predict', 
    "predict.id_predict = portion.id_predict AND event.id_event = 5", 
    'left' 
); 

結果:

LEFT JOIN `predict` ON 
    `predict`.`id_predict` = `portion`.`id_predict` 
    AND event.id_event = 5 

但它應該是更容易:

LEFT JOIN `predict` ON (
    `predict`.`id_predict` = `portion`.`id_predict` 
    AND `event`.`id_event` = 5 
) 

有什麼辦法防止這種錯誤呢?

+0

@RaphaëlAlthaushttp://ellislab.com/forums/viewthread/152221/檢查後仍然患有視力障礙? – motto

+0

問題是第二個子句中缺少反引號還是丟失了括號?我想知道我的視力是否也在玩,因爲我想這兩種形式都會返回相同的結果。 – halfer

+1

實際問題是什麼?損壞/錯誤的數據返回?一個錯誤?後面的勾號不應該對查詢產生影響,但允許您使用非標準的標識符名稱。即使'event.id_event'沒有反標,它也不應該影響結果。額外的括號並不需要有一個有效的SQL語句,儘管它可能有助於將您的意圖傳達給其他人閱讀該語句。 – Wolf

回答

1
$this->db->join(
    'predict', 
    "(predict.id_predict = portion.id_predict AND `event`.`id_event` = 5)", 
    'left' 
); 

你在找這個嗎?

編輯

$this->db->join('predict', "predict.id_predict = portion.id_predict", 'left'); 
$this->db->where('event.id_event = 5'); 
+0

運作良好,沒有pamentalhesis – motto

0

您還可以使用兩個時間

$這個 - > DB->加入(......);
$ this-> db-> join(......);

通過活動記錄多次連接。