think that a save operation should only take into account the passed
data (plus the special fields creatd and modified), looking at the api
and teh core code I can see there is a white list of fields to be
updated, you can specify the array of fields to update when calling
save, but if you omit it i would expect the keys available in the data
that is going to be saved to be used as whitelist.
Call it atomicity, encapsullation or separation of concerns, when you
are working in a complex application, and specially if you follow the
fat model skinny controller approach, you can not be sure of what is
happening behind the scenes in related models, behaviours, callbacks,
filters...
Having to go with something like "Update fields A,B and C of record 2,
but take only into account fields A,B and C" seems very redundant to
me.
I wonder if this is just you and me or other people following the
thread can bring some light.
Regards
On 30 sep, 11:33, WebbedIT <p...@webbedit.co.uk> wrote:
> Ahh, try using:
> $this->User->find('first', array('conditions'=>array('User.id'=>1)));
>
> Instead of:
> $this->User->read(null, 1);
>
> By using Model::read() you're setting the data array at the same time
> as extracting your data.
>
> Extract fromhttp://book.cakephp.org/view/1029/read:
> "read() is a method used to set the current model data (Model::$data)--
> such as during edits ..."
>
> HTH, Paul
>
> On Sep 30, 9:04 am, dtemes <dte...@gmail.com> wrote:> I have created a test function in the controller to illustrate it.
> > Remember that I am updating the user from the groups controller. The
> > relation is User belogs to Group, and Group hasMany User
>
> > function test(){
> > $this->Group->User->recursive=-1;
> > $user1=$this->Group->User->read(null,1);
>
> > $user2=array('User'=>array('id'=>2,
> > 'first_name'=>'New name1',
> > )
> > );
> > DEBUG($user1);
> > DEBUG($user2);
>
> > if ($this->Group->User->save($user2,false)){
> > echo "Save ok";
> > }
> > else
> > {
> > echo "Error saving";
> > }
>
> > $this->render('view');
> > }
>
> > This is the output:
>
> > For $user1:
>
> > Array
> > (
> > [User] => Array
> > (
> > [id] => 1
> > [username] => dtemes
> > [first_name] => xxxxx
> > [last_name] =>
> > [email] => xxxx...@gmail.com
> > [created] => 2010-02-05 23:59:21
> > [password] => xxxxxx
> > [enabled] => 1
> > [role_id] => 1
> > [modified] => 2011-09-25 23:15:34
> > [activation_key] => d6e1f8fe5b5a40d5b403bb596f2cece0
> > [payment] => 1
> > [tutor1] =>
> > [tutor2] =>
> > [tutor3] =>
> > [position] => 5
> > [optout] => 0
> > [country] =>
> > [school_id] => 1
> > [group_id] => 1
> > )
>
> > )
>
> > For $user2:
>
> > Array
> > (
> > [User] => Array
> > (
> > [id] => 2
> > [first_name] => New name1
> > )
>
> > )
>
> > and finally, the update query.....
>
> > UPDATE `users` SET `id` = 2, `username` = 'xxxxx', `first_name` = 'New
> > name1', `last_name` = '', `email` = 'xxx...@gmail.com', `created` =
> > '2010-02-05 23:59:21', `password` = 'xxxxxx', `enabled` = 1, `role_id`
> > = 1, `modified` = '2011-09-25 23:15:34', `activation_key` =
> > 'd6e1f8fe5b5a40d5b403bb596f2cece0', `payment` = 1, `tutor1` = '',
> > `tutor2` = '', `tutor3` = '', `position` = 5, `optout` = 0, `country`
> > = '', `school_id` = 1, `group_id` = 1 WHERE `users`.`id` = 2
>
> > Regards
>
> > On 29 sep, 08:48, WebbedIT <p...@webbedit.co.uk> wrote:
>
> > > So you're re-checking UserA on every page load to compare if anything
> > > hs changed, it would be more efficient to update the session after a
> > > user's profile has been updated, then you only need to refer to the
> > > session.
>
> > > I am 99.9% certain this is not a bug as I update data in this way all
> > > the time.
>
> > > - How are you reading the UserA data before saving UserB?
> > > - Can you show us these lines of code in your controller?
> > > - If you comment out the reading of UserA before save does the data
> > > array save correctly?
> > > - Have you echoed $this->data before and after save to see what it
> > > contains?
>
> > > HTH, Paul
>
> > > On Sep 28, 9:55 pm, dtemes <dte...@gmail.com> wrote:> You are right that userA data is available in the session, and even
> > > > accessible via $this->Auth, but some userA data can be modified
> > > > outside of cake (or a cake task run via cron for instance) so I need
> > > > to read userA before saving userB.
>
> > > > From my point of view a call to model->save where the data passed
> > > > contains the primary key is to be considered an update, and as such,
> > > > only fields in the data array should be updated (plus the modified
> > > > field if it exists in the database) .
>
> > > > Cake experts out there, please advise if I should try to build a test
> > > > case and file a bug.
>
> > > > Regards
>
> > > > On 28 sep, 10:00, WebbedIT <p...@webbedit.co.uk> wrote:
>
> > > > > Login should be an action conducted at least two page requests before
> > > > > you edit UserB and as such why are you reading UserA's data when
> > > > > saving UserB's edited data?
>
> > > > > Once a user is authenticated their data is readily available in the
> > > > > session, sounds like your doing an extra call and putting that data
> > > > > into $this->data at some point.
>
> > > > > HTH, Paul.
>
> > > > > On Sep 27, 12:25 pm, dtemes <dte...@gmail.com> wrote:
>
> > > > > > 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:
Post a Comment