Best way to update a record?

Hello,




foreach($items as $item)

{		

  $id = $item['id'];

  $qty = $item['qty'];

			

  $product = Product::model()->findBySql('select Item, On_hand_qty from shopinventory where Item   =:p1',array(':p1'=>$id));

  $dbqty = $product->On_hand_qty - $qty;

  $product->On_hand_qty = $dbqty;

  $product->save(); 

}



My primate key is character.

Is there a cleaner way to update a record?

Thanks

Yup.




foreach($items as $item)

{               

  $id = $item['id'];

  $qty = $item['qty'];

       

  Product::model()->updateCounters(array('qty'=>-1*$qty), 'id=:id', array(':id'=>$id));

}



See http://www.yiiframework.com/doc/api/1.1/CActiveRecord#updateCounters-detail

:)

Cool! Thanks