Search code examples
phpactiverecordyii

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


Solution

  • 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();