0

I'm trying to update a row on my profiles table to reset a users profile picture to the default of user.png. I have the following action in my controller:

public function deleteProfilePicture() {
    $this->layout = 'ajax';

    // First find the profile ID from the user ID
    $profileId = $this->Profile->find('first', array(
        'condition' => array('User.id' => $this->Auth->user('id')),
        'fields' => array('Profile.id'),
        'recursive' => -1
    ));

    $this->Profile->id = $profileId['Profile']['id'];
    $this->Profile->saveField('picture', 'user.png', false);
}

However, when I request the URL (/profile/deleteProfilePicture) I get no errors but the database row isn't updated. I have made sure the current profile ID is used by using debug($profileId).

What could be going wrong here?

Edit: The return value of saveField():

array(
    'Profile' => array(
        'id' => '36',
        'modified' => '2013-04-05 14:16:57'
    )
)
  • What's the return value of saveField() call? It's likely you have a model or behavior beforeSave() callback which returns false thereby preventing the update. – ADmad Apr 05 '13 at 12:13
  • check the generated sql and try executing it directly on the DB.. maybe there's something wrong there – pleasedontbelong Apr 05 '13 at 12:18
  • I've edited the return value of `saveField()` into the original question. @pleasedontbelong how would I find the SQL it's executing? – user1788996 Apr 05 '13 at 12:19
  • maybe off topic, but it should read 'conditions' not 'condition' in your find() – Justin T. Apr 05 '13 at 12:22
  • Strange behaviour, the `modified` field of the row gets updated, but not `picture`. :/ – user1788996 Apr 05 '13 at 12:22
  • 1
    Seems cake is not "seeing" your picture field. Make sure your field name casing is exactly the same. Clear model cache and check. – ADmad Apr 05 '13 at 12:23
  • http://stackoverflow.com/questions/3647065/how-can-i-see-cakephps-sql-dump-in-the-controller or you could temporaly add the sqldump element on the ajax layout – pleasedontbelong Apr 05 '13 at 12:26
  • Update, looks like the `beforeSave()` method is stopping it from saving. Is there a way to bypass beforeSave()? – user1788996 Apr 05 '13 at 12:33
  • Use the [third param](http://api.cakephp.org/2.3/class-Model.html#_saveField) of saveField() and set 'callbacks' option to false. – ADmad Apr 05 '13 at 13:31

4 Answers4

1

Try

$this->Profile->id = $profileId['Profile']['id']; $this->Profile->set(array( 'picture' => 'user.png' )); $this->Post->save();

Nisanth Sojan
  • 1,099
  • 8
  • 21
1

I see no error in your code. Try seeing what query is getting executed using this

$log = $this->Model->getDataSource()->getLog(false, false);
  debug($log);

based on the result make changes to your query if you find something wrong.

Or try using this

$data['Profile']['id']=$profileId['Profile']['id'];
$data['Profile'['picture']='user.png';
$this->Profile->save($data);
0

If you set debug to 2 you could see what SQL is being executed, see if your update is actually firing.

toby1kenobi
  • 1,609
  • 1
  • 14
  • 24
  • The query being executed only updates the `modified` field for some reason, it's not touching `picture` at all, despite me explicitly telling it to change it. – user1788996 Apr 05 '13 at 12:29
0

Try this

$this->Profile->read(null, $profileId['Profile']['id']);
$this->Profile->saveField('picture', 'user.png', false);

Why are you setting the validation to false? Do you get an error if you omit that?

Nunser
  • 4,512
  • 8
  • 25
  • 37