2014-02-11 101 views
0

我想建立一个更新个人资料页面,我有2个MySQL表1.帐户2.信息,所以我想插入或更新用户信息表。更新个人资料yii框架

这是我迄今为止,但其保存数据"Call to undefined method stdClass::save()"时返回错误

控制器:

public function actionUpdate_profile() { 
     $model = new UserProfileForm(); 

     $user = Userinfo::model()->findByPk(Yii::app()->user->id); 

     $model->name = $user->_name; 
     $model->myurl = $user->myurl; 

     if (isset($_POST['UserProfileForm'])) { 
      $model->attributes = $_POST['UserProfileForm']; 

      if ($model->validate()) { 
       $model->attributes = $_POST['UserProfileForm']; 

       $user->name = $model->name; 
       $user->myurl = $model->myurl; 
       $user->save(); 
      }); 

     }); 

     $this->render('update_profile', array(
      'model' => $model, 
      'user' => $user, 
     )); 
    } 

型号:

class Userinfo extends CActiveRecord { 

    public static function model($className = __CLASS__) { 
      return parent::model($className); 
     } 

    public function tableName() { 
     return 'user_info'; 
    } 

    public function rules() { 
     return array(
      array('email, name, myurl', 'length', 'max' => 255), 
     ); 
    } 

    public function attributeLabels() { 
     return array(
      'user_id' => Yii::t('yii', 'Id User'), 
      'name' => Yii::t('yii', 'First Name'), 
      'myurl' => Yii::t('yii', 'Last Name'), 
     ); 
    } 

class UserProfileForm extends CFormModel { 

    public $name; 
    public $myurl; 

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

    public function attributeLabels() { 
     return array(
      'name' => Yii::t('yii', 'First Name'), 
      'myurl' => Yii::t('yii', 'Last Name'), 
     ); 
    } 

} 
+0

确保Userinfo :: model() - > findByPk(Yii :: app() - > user-> id)'在使用它之前实际找到了一个对象。 – DCoder

+0

为了什么目的,你创建'$ model = new UserProfileForm();'并且没有任何与该模型有关的事 –

回答

0

我知道这不是最好的做法但它很简洁 -

if($user = Userinfo::model()->findByPk(Yii::app()->user->id)) { 
    //then work with $user here 
}