Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii Indirect modification of overloaded property

$winnerBid = Bids::model()->find($criteria);

Model has next relations:

public function relations() {
        return array(
            'item' => array(self::BELONGS_TO, 'Goods', 'item_id'),
            'room' => array(self::BELONGS_TO, 'Rooms', 'room_id'),
            'seller' => array(self::BELONGS_TO, 'RoomPlayers', 'seller_id'),
            'buyer' => array(self::BELONGS_TO, 'RoomPlayers', 'buyer_id'),
        );
    }

When I am trying to save:

 $this->seller->current_item++;
    $this->seller->wins++;
    $this->seller->save();

I am getting error:

Indirect modification of overloaded property Bids::$seller has no effect (/var/www/auction/www/protected/models/Bids.php:16)

But it was everything fine at another server? How to fix it? Or override php directives? Any ideas? TNX

like image 298
Joeeee Avatar asked Mar 17 '11 09:03

Joeeee


2 Answers

The problem here is that $seller is not a "real" property (Yii implements properties on its Models by using the magic __get method), so in effect you are trying to modify the return value of a function (which has no effect). It is as if you tried to do:

function foo() {
    return 42;
}

// INVALID CODE FOR ILLUSTRATION
(foo())++;

I 'm not sure about the status of this behavior on different PHP versions, but there is an easy workaround you can use:

$seller = $this->seller;
$seller->current_item++;
$seller->wins++;
$seller->save();
like image 92
Jon Avatar answered Oct 19 '22 05:10

Jon


I was also having the error message "Yii Indirect modification of overloaded property" when trying to massively manipulate attributes using the CActiveRecord attributes property.

Then, I discovered another method to overcome this issue, in a case where the magic method is related to an object variable which contains an array take a look: you create an AUXILIARY ARRAY in which you put the original and the new values (sometimes one wants to REPLACE a value related to one of the keys, and these methods are not satisfactory). And AFTERWARDS use an assignation, which works like the reference. For example:

$auxiliary_array = array();
foreach(Object->array_built_with_magic as $key=>$value) {
     if(….) {
      $auxiliary_array[$key] = Object->array_built_with_magic[$key];
      } else if (…) {
      $auxiliary_array[$key] = $NEW_VALUE
      }
}
//So now we have the array $auxiliary_array with the
// desired MIX (that is, some originals, some modifications)
//So we will do now:
Object->array_built_with_magic =$auxiliary_array;
like image 23
David L Avatar answered Oct 19 '22 03:10

David L