add or subtract value to a database field

34 views
Skip to first unread message

hema malini

unread,
Aug 24, 2015, 7:07:06 AM8/24/15
to NotORM
what is the equivalent of 

$qty = 3;
UPDATE products SET `available` = available - $qty WHERE (id = '3') AND (bagid = '3')   

?

hema malini

unread,
Aug 24, 2015, 7:18:52 AM8/24/15
to NotORM
In notorm,

 $avail_data = array(
               'available' => "available - $quantity",
       );
 $availqty_update = $this->orm->products(array('id' => $id, 'bagid' => $bagid))->update($avail_data);


this makes the available field value 0.

Allysson David

unread,
Aug 24, 2015, 7:45:10 AM8/24/15
to not...@googlegroups.com
If we take into consideration that you probably don't want to do a select before the update...
Then use a NotORM_Literal:

$avail_data = array(
    'available' => new NotORM_Literal('available - ?', $quantity),
);

hema malini

unread,
Aug 25, 2015, 1:35:25 AM8/25/15
to NotORM
Thank you.
    Is it a select before the update is important? 

Allysson David

unread,
Aug 25, 2015, 6:35:02 AM8/25/15
to not...@googlegroups.com
No. I only said that because if you already had the value from a
previous select you could calculate the subtraction on the php and
update it directly. Yours was a valid question.

2015-08-25 2:35 GMT-03:00, hema malini <svasa...@gmail.com>:
>
>>
>> Thank you.
>>
> Is it a select before the update is important?
>
> --
> You received this message because you are subscribed to the Google Groups
> "NotORM" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to notorm+un...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
Reply all
Reply to author
Forward
0 new messages