Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yii model->save not working for update value

Tags:

php

mysql

yii

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] => 
)
like image 284
uiTeam324 Avatar asked Jan 29 '26 21:01

uiTeam324


2 Answers

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());
}
like image 112
MH2K9 Avatar answered Feb 01 '26 11:02

MH2K9


I'd reccommend to check possible errors on saving:

istead of

$user->save();

you could use

if (!$user->save()){
  print_r($user->getErrors());
}
like image 34
zuups Avatar answered Feb 01 '26 09:02

zuups



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!