2012-06-06 49 views
0

我在笨一些代碼,看起來像這樣:笨/ Active Record的類生成空白()

$this->db->select('email_account_id'); 
$this->db->where('TRIM(LOWER(email_account_incoming_username))',trim(strtolower($email_address))); 
$query = $this->db->get($this->users_db.'email_account'); 

拋出這個錯誤:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') AND TRIM(LOWER(email_account_incoming_username)) = '[email protected]'' at line 3 
SELECT `email_account_incoming_username`, `email_account_id` FROM (`crmuat_users`.`email_account`) WHERE `email_account_id` IN() AND TRIM(LOWER(email_account_incoming_username)) = '[email protected]' 

現在我的生活不我瞭解「IN()」是如何進入的?

任何人都能看到?

+0

我可能只需要重新初始化該類? –

+1

SELECT'email_account_incoming_username'這不在代碼中...? – Bruce

回答

2

您必須在某處調用$this->db->where_in()方法。 CodeIgniter Active Record不可能生成IN()子句,除非您告訴它這樣做。試着看看你在選擇之前是否意外地調用了該方法。還請注意,由於Active Record類在生成查詢時具有這種奇異特性,因此可能您已使用另一種使用Active Record類的方法在另一個地方調用該方法。

例如,以下操作將導致在最終查詢中生成IN()子句。

function a() 
{ 
    ... 
    $this->db->where_in(); 
    ... 
    $this->b(); 
} 

function b() 
{ 
    // Produces the same error as stated in the question because the call of 
    // where_in() beforehand. 
    $this->db->select(...)->where(...)->get(...); 
} 

P.S:你爲什麼要修剪並將字符串轉換爲小寫字母兩次?對我來說似乎是多餘的。

+0

我明白你在說什麼,並同意它是問題,但是有沒有辦法可以重新初始化它或清除它的緩存?例如也許類似於$ this-> db-> flush_cache()? –

+1

不幸的是,沒有。您實際上可以將該類的所有屬性重置爲原始狀態,因爲它們的訪問修飾符是公開的。但是,如果我是你,我不會那樣做。因爲它似乎不是一個好的做法。它被寫入公開的唯一原因(使用'var'關鍵字)是爲了向後兼容PHP 4(我認爲)。所以如果你這樣做,代碼可能會在未來破裂(如果團隊決定使用私有/受保護的)。還有一些可用於重置這些屬性的類的方法。但是,它們的訪問修飾符是受保護的。 –