PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, January 12, 2022

[FIXED] Indirect modification of overloaded property to modify post data

 January 12, 2022     forms, yii     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing