在 cakephp 3 中更改对象 属性 值

Change object property value in cakephp 3

我的索引操作就像

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;
    });