在更新时,跳过更新yii的某些属性

dev*_*234 6 php model yii

我需要停止更新某些值,即使那些设置为POST数组.这样做我在yii规则中使用不安全.

array('id', 'unsafe', 'on'=>'update'),
Run Code Online (Sandbox Code Playgroud)

仍然有这个,我无法跳过更新ID.

怎么能用yii完成?

下面是我的规则功能..

public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('name, body, created_date', 'required'),
        array('name', 'length', 'max'=>128),
        array('body', 'length', 'max'=>512),
        array('id', 'unsafe', 'on'=>'update'),
        // The following rule is used by search().
        // @todo Please remove those attributes that should not be searched.
        array('id, name, body, created_date', 'safe', 'on'=>'search'),
    );
}
Run Code Online (Sandbox Code Playgroud)

更新1

$ model-> attributes = $ _POST ['User'];

我在保存时需要跳过某些属性.

$模型 - )>保存(;

Man*_*uer 2

当您在控制器中创建新的模型实例时,您将需要声明场景,例如,如果您的声明是这样的

$myModelInstance = new MyModel();
Run Code Online (Sandbox Code Playgroud)

你需要将其更改为

$myModelInstance = new MyModel('update');
Run Code Online (Sandbox Code Playgroud)

但是,如果您使用活动记录的查找方法之一来保存它,那么它会自动设置为“更新”,如下所示: http: //www.yiiframework.com/doc/api/1.1/CActiveRecord#save-detail

如果您使用其他逻辑来声明模型,您可以简单地使用 setScenario 函数

$myModel->setScenario("update"); 
Run Code Online (Sandbox Code Playgroud)