Tuesday, March 1, 2011

Re: Help with update

On Mar 1, 2011, at 11:35, lirc201 wrote:

> I like the example on the weblink which is just using MySQL
> functionality, it is very clever. Thanks for the help and push. My
> code is working! I did like the security tricks I discovered for my
> updates below to only pass a field list.
>
> function updateInventory($id, $quantity) {
> $inv = $this->getQuantity($id); <- function is in the same model
> $this->data['Model']['id'] = $id;
> $this->data['Model']['inventory'] = $inv + $quantity;
> if($this->save($this->data, array(
> 'validate' => 'false',
> 'fieldList' => array('id', 'inventory')))) {
> ... logic for return values ...
> }
> }

This is not atomic. Between the line where you get the current quantity and the line where you save the updated quantity, someone else might have updated the quantity, and you'd be wiping out their change. Consider two managers simultaneously adding 5 to the inventory of the same item. The expected result would be that in the end the inventory is incremented by 10, but because of the race condition in your code, it might only be incremented by 5.

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: