How to change $model->attributes value in controller - Yii

喜你入骨 提交于 2019-12-01 04:58:18

问题


UserMasterController Code:

public function actionUpdate($id){

    $model=$this->loadModel($id);

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

        if($model->save())
            $this->redirect(array('admin'));
    }

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

the line which gives me an error is : $model->attributes['emailsent'] = 'N';

ERROR : Indirect modification of overloaded property UserMaster::$attributes has no effect

How can I change the attribute value ? I just want to set it as 'Y' or 'N' as per the condition


回答1:


Use $model->emailsent='N';. Thats all




回答2:


Just try this $model->setAttribute($name,$value);



来源:https://stackoverflow.com/questions/10511533/how-to-change-model-attributes-value-in-controller-yii

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!