zu3i4ot
(Zu3i4ot)
February 11, 2011, 7:17am
1
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?
andy_s
(Arekandrei)
February 11, 2011, 7:41am
2
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...
}
eval
(Zamanfoo)
February 11, 2011, 9:30am
3
[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]
zu3i4ot
(Zu3i4ot)
February 11, 2011, 10:22am
5
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…
w4nt3d
(Vlad Velici)
February 13, 2011, 3:32pm
6
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();
}
rickgrana
(Ricardo Grana)
February 14, 2011, 3:19am
7
public function afterSave(){
if (parent::afterSave()){
$profile = new Profile;
$profile->user_id = $this->id;
$profile->display_name = $this->username;
return $profile->save();
}
return false;
}
andy_s
(Arekandrei)
February 14, 2011, 5:03am
8
Unlike beforeSave() or beforeValidate() methods, afterSave() doesn’t return true or false.