Tuesday, March 1, 2011

Re: Help with update

Here is the updated code. I've deciding on increasing or decreasing
in the calling controller. If you want to decrease the inventory
numbers than pass a negative $quantity. This was the interface I
chose because I will have several different controllers calling this
function to make updates to the inventory numbers, like when more
inventory is added.

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 ...
}
}


On Mar 1, 12:03 pm, cricket <zijn.digi...@gmail.com> wrote:
> On Tue, Mar 1, 2011 at 10:42 AM, lirc201 <brian.e.lave...@gmail.com> wrote:
> > Hello,
>
> > I'm trying to perform an update to my model in order to reflect a
> > change in the inventory value.  The values that I'm passing to $id,
> > and $quantity below are correct, but even with a hard coded value of 5
> > the database still gets updated with a 0 for the inventory field for
> > the given id.  This leads me to think I'm not passing the information
> > correctly to $this->save as I was using an update example from the 1.3
> > book for Saving Data.
>
> > In Model:
>
> > function updateInventory($id, $quantity) {
>
> > $this->id = $id;
> > $this->inventory = '5';
> > if($this->save($this->data, array('validate' => 'false',
> >                                            'fieldList' => array('id', 'inventory')))) {
> > .....
>
> > }
>
> There is no $inventory class member. There is an $id, though. What you
> need to do is construct an array and pass that to save(). Using
> $this->data is fine but it's not apparent here that it contains the
> correct data at all. The array should look like:
>
> array(
>         'YourModelName' => array(
>                 'id' => $id,
>                 'inventory' => $inventory
>         )
> )
>
> But there are simpler ways to do this:http://nuts-and-bolts-of-cakephp.com/2008/05/22/incrementing-a-field-...
>
> Also, is the $quantity param the number of items that inventory has
> changed by? How are you deciding whether to increase or decrease by
> that amount?

--
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: