在更新时,跳过更新yii中的某些属性
i need to stop updating certain value even those are set to POST array. to do that i am using unsafe in yii rules.
我需要停止更新某些值,即使它们被设置为POST数组。为此,我在yii规则中使用了不安全。
array('id', 'unsafe', 'on'=>'update'),
still with this, i am unable to skip the id from updating.
尽管如此,我还是无法跳过更新id。
how can this be done with yii?
如何用yii实现这一点?
below is my rules function..
下面是我的规则函数。
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'),
);
}
Update 1
更新1
$model->attributes = $_POST['User'];
$模型- >属性= $ _POST['用户”);
and i need when saving, to skip certain attributes.
保存时,我需要跳过某些属性。
$model->save();
$模型- >保存();
2 个解决方案
#1
2
When you are creating the new model instance in your controller you will need to declare the scenario for example if your declaration was something like this
当您在控制器中创建新的模型实例时,您将需要声明场景,例如,如果您的声明是这样的
$myModelInstance = new MyModel();
you will need to change it to
您需要将其更改为
$myModelInstance = new MyModel('update');
However if you are using one of the find methods of active records to save it then it is set automatically to "update" as here: http://www.yiiframework.com/doc/api/1.1/CActiveRecord#save-detail
但是,如果您正在使用活动记录的一个查找方法来保存它,那么它将自动设置为“更新”:http://www.yiiframework.com/doc/api/1.1/CActiveRecord#save-detail。
if you are using some other logic for declaring the model you can simply use the setScenario function
如果您正在使用其他逻辑来声明模型,您可以使用setScenario函数
$myModel->setScenario("update");
更多相关文章
- 【转】phpcms-v9中关于模型的理解
- thinkphp5 数据库和模型详解 之1 数据库基础
- yii2的AR模型对id自动去重的问题
- 为什么只有ASP.NET有异步编程模型?
- php 23种设计模型 - 抽象工厂模式
- [CI]CodeIgniter视图 & 模型 & 控制器
- Laravel在模型保存中两次插入差异
- 在SQL中声明@变量返回错误
- 从模型到控制器并返回到模型的数据