开发者

Yii update profile page

i created a update profile page.

i have this in the contro开发者_StackOverflow社区ller to populate the form and also handle update:

$user = User::model()->findByPk(Yii::app()->user->id);
// Collect user input 
if (isset($_POST['User'])) {
 $user->attributes = $_POST['User'];

 if ($user->save()) {
  echo "update successfully";
 }
 else {
  echo "update failed";
 }
}

// View
$this->render('user_view', array('user'=>$user,));

however, this doesn't work. although $user->save is true, the record is not updated in the database. i've also check that $_POST['User'] is returning the updated data but $user->attributes is not saving them.

why is that so?


You need to set which model attributes are "safe" for "massive assignments". Read more about this here.

The mass attribute assignment $user->attributes will only assign to variables with validation rules. Just give the name attribute a rule, even if it's just the "safe" validator.

public function rules()
{
  return array(
    array('name', 'safe')
  );
}

I'm pretty sure this is the problem you are having, it's happened to me!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜