Tuesday, September 27, 2011

Re: Model update modifies fields not present in data array

It's part of the same query, no after/beforeSave running.

The model in question is the User model. These are my steps

1. Read the logged in user (Lets call it UserA)
2. Select another user from the user table to update (UserB).
3. Present a form with just a few fields that I need to update
4. Update UserB using the data in the form.

If I follow this pattern UserB fields not present in the data array
are filled with data from UserA

If I call User->create() between steps 3 and 4, then no data from
UserA is passed to UserB, but fields not present in the data array
coming from the form are reset to the database default value.....

The only solution is to read UserB before step 4.

On 26 sep, 12:30, WebbedIT <p...@webbedit.co.uk> wrote:
> Is the update of the enabled field happening as part of the same SQL
> query or a separate one?  Are you sure you have not got a callback
> (beforeSave or afterSave) running which is doing this?
>
> HTH, Paul
>
> On Sep 25, 7:20 pm, dtemes <dte...@gmail.com> wrote:> More information, If i don't provide a value for fields that have a
> > default value in the database, the fields are modified and they take
> > the database default value.
>
> > For instance I have a reset password action that takes data from a
> > form with just the user id and password. The data array only has id
> > and password.
>
> > Submitting the form changes the user password, but the user model has
> > a enabled field, with a default value of 0. In the sql query I can see
> > that cake is doing an update of the password fields but also of the
> > enabled field.
>
> > Using cake 1.3.5
>
> > On 25 sep, 15:31, dtemes <dte...@gmail.com> wrote:
>
> > > I ahev to related models, group and user, and I have a form to update
> > > user data from the groups controller, previously to the User->Save i
> > > read some information from the current user, the code is something
> > > like:
>
> > > $this->Group->User->read->($this->Auth->user('id'));
> > > .
> > > .
> > > .
> > > $this->Group->User->Save($this->data);
>
> > > As a result if the current user value of fieldX is 123, then the
> > > fieldX of the user being updated takes the value 123, but there is no
> > > such fieldX in the $this->data
>
> > > One way to avoid this is by doing a  $this->Group->User->read(null,
> > > $this->data['User']['id']); but either there is something wrong in the
> > > cake implementation of the save function or most probably I am missing
> > > something.
>
> > > Any ideas?

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