即使那些设置为 POST 数组,我也需要停止更新某些值。为此,我在 yii 规则中使用了 unsafe。
array('id', 'unsafe', 'on'=>'update'),
仍然如此,我无法跳过 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'),
);
}
更新 1
$model->attributes = $_POST['User'];
我需要在保存时跳过某些属性。
$模型->保存();
最佳答案
当您在 Controller 中创建新模型实例时,您需要声明场景 例如 如果你的声明是这样的
$myModelInstance = new MyModel();
你需要把它改成
$myModelInstance = new MyModel('update');
但是,如果您使用事件记录的一种查找方法来保存它,那么它会自动设置为“更新”,如下所示:http://www.yiiframework.com/doc/api/1.1/CActiveRecord#save-detail
如果您使用其他一些逻辑来声明模型,您可以简单地使用 setScenario 函数
$myModel->setScenario("update");
关于php - 更新时,跳过更新 yii 的某些属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20114179/