Search code examples
phpsqlsymfony1doctrine

Calculation in symfony doctrine update query


We have a situation in which we need to update records based on existing data in table, this is query which i created manually, just to explain this query updates prize field data using amount field by subtracting its 51 percent value.

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

here is my symfony code

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

Solution

  • That easy to do, there is even examples in the documentation.

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

    The DQL parser is smart enouth to detect colums in the set part.

    By the way, do not use ->where('id =1') but ->where('id = ?', 1).