Cakephp 3在foreach中保存相关数据

时间:2016-02-26 14:16:13

标签: foreach cakephp-3.0

如何使用$item = $this->Items->newEntity();在foreach循环中保存来自$this->request->data的数据。

 $item = $this->Items->newEntity();
 foreach ($array as $item) {

        //added for sake of answer
        $item = $this->Items->newEntity();
        $this->request->data['objects']['_ids'][] = $item['object_id'];
        $this->Items->patchEntity($item, $this->request->data);
        $this->Items->save($item); 
        //added
        unset($this->request->data['object']['_ids']);

        }

newEntity()超出foreach时,有一个item.id和正确数量的object.ids但与同一item.id有关。当我将newEntity()设置为foreach循环内部时,我的item.ids数量正确,但表object.iditems_object的数量不正确(超过了数组)。

答案:

newEntity()放在foreach中,将unset($this->request->data['object']['_ids']);放在最后。它似乎产生了正确的条目。

0 个答案:

没有答案