Object 更改cakephp 3中的对象属性值

Object 更改cakephp 3中的对象属性值,object,cakephp-3.0,Object,Cakephp 3.0,我的索引动作就像 public function index() { $acos = $this->Acos->find('threaded'); foreach ($acos as $aco) { $aco->children = doSomeOperations($aco->children); } } 我想用它的新值替换$acos->$aco->children值,但我不能这样做您只需要使用引用运算符 public func

我的索引动作就像

public function index() {
    $acos = $this->Acos->find('threaded');
    foreach ($acos as $aco) {
        $aco->children = doSomeOperations($aco->children);
    }
}

我想用它的新值替换$acos->$aco->children值,但我不能这样做

您只需要使用引用运算符

public function index() {
    $acos = $this->Acos->find('threaded');
    foreach ($acos as &$aco) {
        $aco->children = doSomeOperations($aco->children);
    }
}
另一种方法是使用结果集中的收集方法:

$acos = $this->Acos->find('threaded')
    ->map(function ($aco) {
        $aco->children = doSomeOperations($aco->children);
        return $aco;
    });

谢谢亲爱的@Jose。我测试了您的解决方案,但第一个解决方案导致此错误“迭代器不能与foreach按引用一起使用”,第二个解决方案导致“不允许序列化‘Closure’”,您在哪里序列化闭包?最后可以调用
->toArray()
,以确保不序列化闭包。如果不是你自己做的,请确保你已经将DebugKit插件更新到了它的最新版本,因为这是一个已知的bug。你是对的。我禁用了debugkit,它工作正常。1) 你能给我一个直接的链接来下载debugkit吗。我无法访问cmd来使用“php composer.phar require cakephp/debug_kit”2)您猜第一个解决方案中的&$aco有什么问题吗?问题是查询返回的是迭代器而不是数组。但是您可以调用
->toArray()
,这样您就可以像普通数组一样进行迭代和更改。