【问题标题】:Calculation in symfony doctrine update querysymfony 学说更新查询中的计算
【发布时间】:2012-01-17 09:45:45
【问题描述】:

我们有一种情况,我们需要根据表中的现有数据更新记录,这是我手动创建的查询,只是为了解释这个查询使用金额字段减去其 51% 的值来更新奖品字段数据。

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();

【问题讨论】:

    标签: php sql symfony1 doctrine


    【解决方案1】:

    这很容易做到,甚至还有例子in the documentation

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

    DQL 解析器非常聪明,可以检测集合部分中的列。

    顺便说一句,不要使用->where('id =1'),而是使用->where('id = ?', 1)

    【讨论】:

      【解决方案2】:

      我希望我做对了。很简单:

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

      【讨论】:

      • 我认为amount 是一个表格列,而不是一个变量。但是这个问题并不准确:(
      猜你喜欢
      • 2011-09-11
      • 1970-01-01
      • 1970-01-01
      • 2011-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多