uiTeam324
uiTeam324

Reputation: 1245

yii model->save not working for update value

public function actionUpdateprofile(){
    $user = User::model()->findByPk($_POST['User']['user_id']);
    $profile = Profile::model()->findByPk($_POST['User']['user_id']);

    if(isset($_POST['User']) || isset($_POST['Profile'])){
        $user->attributes = $_POST['User'];
        $profile->attributes = $_POST['Profile'];
        $user->save();
        $profile->save();

    }
}

I did this code for update the value in profile and user table. But it's not working. If i send $_POST['Profile']['email'] = '[email protected]'; There is no error but database still showed old value. why?

What i did wrong in there?

This is the result for $profile->attributes. email still have old value.

Array
(
    [user_id] => 35
    [lastname] => asd
    [firstname] => asrtyr
    [email] => xyz.gmail.com
    [phoneno] => 123456
    [prof_img] => 
    [phoneno2] => 0
    [agentKey] => 
    [fb_id] => 
)

Upvotes: 2

Views: 5514

Answers (3)

tebazil
tebazil

Reputation: 101

First, do this to check if it has to do with validation (look example below) and test it:

public function actionUpdateprofile(){
    $user = User::model()->findByPk($_POST['User']['user_id']);
    $profile = Profile::model()->findByPk($_POST['User']['user_id']);

    if(isset($_POST['User']) || isset($_POST['Profile'])){
        $user->attributes = $_POST['User'];
        $profile->attributes = $_POST['Profile'];
        $user->save(false);
        $profile->save(false);

    }
}

Which will bypass the validation. If it works, and you really need the validation (and you most certainly do), remove the false's I added and build it up by following the validation guide. The thing to remember is: the property not associated with a rule in rules() array is considered unsafe and is NOT saved into the database. This might be your problem.

Upvotes: 1

zuups
zuups

Reputation: 1150

I'd reccommend to check possible errors on saving:

istead of

$user->save();

you could use

if (!$user->save()){
  print_r($user->getErrors());
}

Upvotes: 1

MH2K9
MH2K9

Reputation: 12039

I suggest you to add error reporting like below

if(!$user->save()){
    echo 'Error to save user model<br />';
    var_dump($user->getErrors());
}
if(!$profile->save()){
    echo 'Error to save profile model<br />';
    var_dump($profile->getErrors());
}

Upvotes: 7

Related Questions