afterSave() not working


 public function afterSave(){

            $profile = new Profile;

            $profile->user_id = $this->id;

            $profile->display_name = $this->username;

            $profile->save();

        }

hi i m using this in order to store the user id and user name in profile but no record is inserted on the profile table from the users table can anyone help?

Make sure there are no validation errors ($profile->errors).

Also don’t forget to call a parent afterSave():




public function afterSave(){

    parent::afterSave();

    // your code...

}



[size="2"]Try changing the signature to[/size]

[size="2"]

[/size]

[size="2"]


protected function afterSave() {}

[/size]

[size="2"]

[/size]

[size="2"]Also like andy_s pointed, make sure you also call parent::afterSave() [/size]

nothing happened

and this


$profile->errors

is giving error.

???


protected function afterSave(){

            parent::afterSave();

            $profile = new Profile;

            $profile->user_id = $this->id;

            $profile->display_name = $this->username;

            if($profile->validate())

                    $profile->save();

            else {

                echo "didn't worked";

                die();

            }

            //$profile->errors();

            return  parent::afterSave();;

        }

this is so far in the users model what i have been messing around…

You are validating $profile two times. Call $profile->save(false); or


if (!$profile->save()) {

   echo "didn't work";

   Yii::app()->end(); // don't use die();

}




 public function afterSave(){

            

        if (parent::afterSave()){

            $profile = new Profile;

            $profile->user_id = $this->id;

            $profile->display_name = $this->username;

            return $profile->save();

        }


        return false;

 }



Unlike beforeSave() or beforeValidate() methods, afterSave() doesn’t return true or false.

$profile->save(false);

worked for me

thanks everyone.