Tuesday, March 1, 2011

Re: Help with update

On Tue, Mar 1, 2011 at 10:42 AM, lirc201 <brian.e.lavelle@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-in-cakephp/

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: