在cakephp 3中更改对象属性值

时间:2015-01-11 11:47:40

标签: object cakephp-3.0

我的索引操作就像

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

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

1 个答案:

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