Issue
My goal is change a $_POST['url'] to save it in database with in front"tcp://".$_POST['url'].
$model = $this->loadModel($id);
if (isset($_POST['xxx'])) {
$model->attributes = $_POST['xxx'];
$model->attributes['url'] = 'tcp://'.$_POST['xxx']; <-
if ($model->save()) {
but it return "Indirect modification of overloaded property " . What the correct way to change that field?
Solution
In this case you have two choices:
1) Because you used $model->attributes = $_POST['xxx'];
, you can access the values in $_POST['xxx']
as the model's attributes, so $model->url = 'something';
will work.
2) Generally you can move the values you want to modify into a new variable, modify them there and overwrite the original value with the new variable. It is especially useful if you want to modify a related model, which results in the same error message you received.
The wrong way:
$model->relationSomething = new RelationSomething;
$model->relationSomething->someAttribute = 'newValue';
The code above will result in the error message you received.
The correct way:
$model->relationSomething = new RelationSomething;
$tempVariable = $model->relationSomething;
$tempVariable->someAttribute = 'newValue';
$model->relationSomething = $tempVariable;
//Optimally you want to save the modification
Using this method lets you modify attributes in related models without causing errors.
Answered By - Adam Benedek
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.