我的索引操作就像
public function index() {
$acos = $this->Acos->find('threaded');
foreach ($acos as $aco) {
$aco->children = doSomeOperations($aco->children);
}
}
我想用新值替换$ acos-> $ aco-> children值,但我不能这样做
答案 0 :(得分:1)
您只需要使用引用运算符
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;
});