间接修改重载属性修改post数据
Indirect modification of overloaded property to modify post data
我的目标是更改 $_POST['url'] 以将其保存在前面的数据库中 "tcp://".$_POST['url'].
$model = $this->loadModel($id);
if (isset($_POST['xxx'])) {
$model->attributes = $_POST['xxx'];
$model->attributes['url'] = 'tcp://'.$_POST['xxx']; <-
if ($model->save()) {
但它 return "Indirect modification of overloaded property " 。
更改该字段的正确方法是什么?
在这种情况下,您有两个选择:
1) 因为您使用了 $model->attributes = $_POST['xxx'];
,您可以访问 $_POST['xxx']
中的值作为模型的属性,所以 $model->url = 'something';
将起作用。
2)一般可以将要修改的值移动到新变量中,在那里修改,然后用新变量覆盖原来的值。如果您要修改相关模型,这将特别有用,这会导致您收到相同的错误消息。
错误的方式:
$model->relationSomething = new RelationSomething;
$model->relationSomething->someAttribute = 'newValue';
上面的代码将导致您收到错误消息。
正确的方法:
$model->relationSomething = new RelationSomething;
$tempVariable = $model->relationSomething;
$tempVariable->someAttribute = 'newValue';
$model->relationSomething = $tempVariable;
//Optimally you want to save the modification
使用此方法可以修改相关模型中的属性而不会导致错误。
我的目标是更改 $_POST['url'] 以将其保存在前面的数据库中 "tcp://".$_POST['url'].
$model = $this->loadModel($id);
if (isset($_POST['xxx'])) {
$model->attributes = $_POST['xxx'];
$model->attributes['url'] = 'tcp://'.$_POST['xxx']; <-
if ($model->save()) {
但它 return "Indirect modification of overloaded property " 。 更改该字段的正确方法是什么?
在这种情况下,您有两个选择:
1) 因为您使用了 $model->attributes = $_POST['xxx'];
,您可以访问 $_POST['xxx']
中的值作为模型的属性,所以 $model->url = 'something';
将起作用。
2)一般可以将要修改的值移动到新变量中,在那里修改,然后用新变量覆盖原来的值。如果您要修改相关模型,这将特别有用,这会导致您收到相同的错误消息。
错误的方式:
$model->relationSomething = new RelationSomething;
$model->relationSomething->someAttribute = 'newValue';
上面的代码将导致您收到错误消息。
正确的方法:
$model->relationSomething = new RelationSomething;
$tempVariable = $model->relationSomething;
$tempVariable->someAttribute = 'newValue';
$model->relationSomething = $tempVariable;
//Optimally you want to save the modification
使用此方法可以修改相关模型中的属性而不会导致错误。