2013-06-05 34 views
4

我才意識到,你不能使用:

$this->db->join() 

$this->db->update() 

看來「加盟「由codeigniter正常執行,但未在查詢中使用,在使用以下命令獲取的基本表更新後出現:$ this-> db-> last_query();

我看到沒有加入。然後我試着對連接表進行更新,認爲只有在需要時纔會使用連接,但我沒有工作並告訴我錯誤1054「未知列XXX在where子句」。

有沒有辦法強制codeigniter?我構建軟件的方式,我真的不想自己構建查詢的所有不同部分(join,where),調用$ this-> db-> query()。

注:我看到這些鏈接:

Codeigniter active record update statement with a join

Is it possible to UPDATE a JOINed table using Codeigniter's Active Record?

codeigniter - database : how to update multiple tables with a single update query

,但如果有人知道一個更清潔的方式,將是很好的,因爲這些解決方案都不能有工作我因爲我在使用連接預覽變更的「preProcessing()」方法中使用了相同的連接,所以同樣的「preProcessing()」方法用於第e更換

+0

我注意到連接語句都在$ this-> db-> ar_join中。也許有辦法強制這些 – NaturalBornCamper

回答

1

好吧,我設法找到一個「乾淨」的解決方案,使用codeigniter的連接,集合等等。所以最酷的是你將擁有使用$ this-> db-> join()的所有CI的好處, $ this-> db-> join()等,就像轉義和添加引號一樣。

所以首先把你所有的CI的東西:

$this->db->join(..) // Set all your JOINs 
$this->db->set(..) // Set your SET data 
$this->db->where(..) // Set all your WHEREs 

然後你就可以建立使用Active Record的準備,清潔的查詢和逃脫查詢元素:

// JOIN 
$sql = "UPDATE $this->baseTable "; 
$sql .= implode(' ', $this->db->ar_join); 

// SET 
$sql .= ' SET'; 
$setArray = array(); 
foreach ($this->db->ar_set as $column=>$newValue) 
    array_push($setArray, " $column = $newValue"); 
$sql .= implode(',', $setArray); 

// WHERE 
$sql .= ' WHERE '.implode(' ', $this->db->ar_where); 

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

如果有人有更好的解決方案,我會很樂意接受它,並用它來代替

相關問題