tags:

views:

247

answers:

2

I need to increment User's balance, so I do:

Doctrine_Query::create()->from('User')->update('balance', 'balance + 0.15')->execute();

And I got an error "Unknown component alias 0". I think its because of 0.15 So how can I update (using DQL) balance without additional SELECT queries to User's table to fetch his balance, calculate new balance and do query like Doctrine_Query::create()->from('User')->update('balance', '?', $new_balance)->execute();

A: 

Your assumption looks right. Doctrine thinks the 0 is a table alias. Have you tried using set()?

From the docs:

$q = Doctrine_Query::create()
    ->update('Account')
    ->set('amount', 'amount + 200')
    ->where('id > 200');
// If you just want to set the amount to a value
$q->set('amount', '?', 500);
echo $q->getSqlQuery();

// UPDATE account
// SET amount = amount + 200
// WHERE id > 200
Mike B
I need to INCREMENT user's balance, not set directly. When using integer values it's all OK, but float is not. Now I testing Doctrine_Query::create()->update('Users')->set('balance', 'balance + "0.15"')->whereIn('id', $ids)->execute(); and it seems to be good solution.
YS-PRO
A: 

This solution works well for MySQL!!!

Doctrine_Query::create()->update('Users')->set('balance', 'balance + "0.15"')->execute();
YS-PRO
Why do you treat number (`0.15`) as a string (`"0.15"`)?
Crozin