更新配置文件yii框架

时间:2014-02-11 06:24:06

标签: php yii

我想构建一个更新配置文件页面,我有2个mysql表1.帐户2.信息,所以我想插入或更新用户信息表。

这是我到目前为止所做的,但在保存数据"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'),
        );
    }

}

1 个答案:

答案 0 :(得分:0)

我知道这不是最佳做法,但它很简洁 -

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