php Yii 对重载属性的间接修改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5337343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Yii Indirect modification of overloaded property
提问by Joeeee
$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)
间接修改重载属性 Bids::$seller 无效 (/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
但是在另一台服务器上一切正常吗?如何解决?或者覆盖 php 指令?有任何想法吗?TNX
回答by Jon
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:
这里的问题是它$seller
不是一个“真正的”属性(Yii 通过使用魔法__get
方法在它的模型上实现属性),所以实际上你试图修改一个函数的返回值(它没有效果)。就好像你试图做:
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:
我不确定这种行为在不同 PHP 版本上的状态,但您可以使用一个简单的解决方法:
$seller = $this->seller;
$seller->current_item++;
$seller->wins++;
$seller->save();
回答by David L
I was also having the error message "Yii Indirect modification of overloaded property"when trying to massively manipulate attributes using the CActiveRecord attributes property.
尝试使用 CActiveRecord 属性属性大量操作属性时,我也收到错误消息“Yii Indirect modify of overloaded 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,在其中放置原始值和新值(有时是一个想 REPLACE 与其中一个键相关的值,这些方法都不尽如人意)。并且 AFTERWARDS 使用分配,其工作方式类似于参考。例如:
$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;