我的索引操作就像
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;
});
关于object - 更改 cakephp 3 中的对象属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27886676/