2012-01-17 24 views
1

我們有一種情況,我們需要根據表中現有的數據更新記錄,這是我手動創建的查詢,只是爲了解釋此查詢更新獎金字段數據使用金額字段通過減去它51%的價值。symfony教條更新查詢中的計算

update users set final_amount = amount-(amount*51/100) where id = 1 

這裏是我的symfony代碼

$q = Doctrine_Query::create()   
    ->update('Users')    
    ->set('final_amount',"'". $amount."'") 
    ->where('id =1')->execute(); 

回答

3

那容易做,甚至有例子in the documentation

$q = Doctrine_Query::create() 
    ->update('users') 
    ->set('final_amount', 'amount-(amount*51/100)') 
    ->where('id = ?', 1)->execute(); 

DQL解析器很聰明,可以在設置的部分中檢測colums。

順便說一下,請不要使用->where('id =1'),而是使用->where('id = ?', 1)

0

我希望我是正確的。它很簡單:

$query = Doctrine_Query::create() 
      ->update('Users') 
      ->set('final_amount', $amount - $amount * 51/100) 
      ->where('id = 1') 
      ->execute(); 
+0

我認爲'amount'是一個表列,而不是一個變量。但問題是不準確的: – Damien 2012-01-18 13:38:00