将sql查询转换为Fluentpdo查询

时间:2016-06-29 10:59:14

标签: php mysql fluentpdo

我有用PHP编写的这个SQL查询:

$query = sprintf("UPDATE bank_info SET
                      amount_dollar = amount_dollar +'$amount_dollar'  ,
                      amount_euro = amount_euro + '$amount_euro' ,
                      amount_local = amount_local + '$amount_local'
                      WHERE bank_id = '$bank_id' ");

此查询工作正常,但我想使用FluentPDO转换此查询。 我想使用数组来设置值。 例如:

$table='bank_info'; //table name
$arrVal=array();    //values needs to be SET
$arrVal['amount_dollar = amount_dollar+?']=$amount_dollar;
$arrVal['amount_euro = amount_euro+?']=$amount_euro;
$arrVal['amount_local = amount_local+?']=$amount_local;
$arrWhere=array();  //where condition
$arrWhere['bank_id']=$bank_id;

这是查询:

$query = $this->pdo->update($table)->set($arrVal)->where($arrWhere);

$query->execute();

我认为问题出现在$ arrVal中,无法找到正确的SET方法并将值添加到表中列的当前值。 我使用数组来选择并从DB /表中获取值多次,所以我认为$ arrWhere不是问题所在。

1 个答案:

答案 0 :(得分:2)

好吧,找到了答案,

代表:

这对我有用:

$id = 5;
$field = 'stock';
$amount = 1;

$increment = array($field => new FluentLiteral($field.' + '.$amount));

$fpdo->update('products')->set($increment)->where('id', $id)->execute();